It's probably a bad idea to ask a question, which already have multiple answers and multiple times, but I should ask it anyway. I tried pretty much everything I find there Prevent redirect after form is submitted but nothing helps me. There is a some minor detail, which I don't see. I'm not very familiar with jQuery and AJAX. Especially with the former. So, the code:
<form id="form" action="uploadfile.php" method="post" enctype="multipart/form-data" ><!--action="uploadfile.php" onsubmit="return false;" --> <label>Name</label> <input id="username" name="username" type="text" onblur="checkUsername(this.value)" onkeypress="clearError('nameerror')" oninput="clearError('nameerror')" /><br> <label id="nameerror"></label><br> <label>Email</label> <input id="email" name="email" type="text" onblur="validateEmail(this.value)" onkeypress="clearError('emailerror')"/><br> <label id="emailerror"></label><br> Select a file<br /> <label id="draganddroperror"></label><br> <input name="fileToUpload[]" id="fileToUpload" type="file" onchange="onChange(event)" multiple /><br /> <button id="btnSubmit" onclick="sendData()" style="background-color: gray; color: #ffffff;" />Отправить</button> </form> There is my JS
function sendData() { var file_data = $("#fileToUpload").prop("files"); console.log(file_data); if ($("#file_data").val() != "") { var form_data = new FormData(); //form_data.append('file', file_data); //console.log(file); form_data.append('file', file_data); console.log(form_data); $.ajax({ url: 'uploadfile.php', // point to server-side PHP script dataType: 'text', // what to expect back from the PHP script, if anything cache: false, contentType: false, processData: false, data: form_data, type: 'post', success: function(data) { // get server responce here //alert(data); // clear file field //$("#your-files").val(""); return false; } }); return false; //event.preventDefault(); } else { alert("Please select file!"); } } So, this is the code in question. All works flawlessly, except redirect. Another questions contains submit, but I didn't have submit input. I tried to delink form from post method (1st line), but I got server error. Return false everywhere. I spent countless hours on this question, it consumed almost all my night hours for a few days. I would appreciate any help, thanks.
return falsebe after theelseclause?<button>is apostaction by itself.<button type="submit"></button>inside your form, every button acts astype="submit"