Form Submit To Upload File And Also Other Fields Using Ajax April 10, 2023 Post a Comment I have tested every question here and have also googled alot, but did not find the something that work. Here is my HTML: Solution 1: I would use FormData for this task. Here is an example of your code using FormData : $(function () { //On dom ready: $("#createform").submit(function (e) { //will be triggered on submit: e.preventDefault(); if( window.FormData !== undefined ) //make sure that we can use FormData ie>9, chrome > 7, opera > 12 safari >5, android > 3 gecko mobile > 2, opera mobile >12 <- wil support XHR too { var formData = new FormData($('#createform')[0]); // use "[0]" <- important // you can append aditional values (regular text): formData.append("be","some value"); $.ajax({ url: 'index/create/createcontrols.php', //Server script to process data type: 'POST', data: formData, xhr: function() { }, success: function(response){ $("#createformresults").text("SUCCESS"); }, error: function (jqXHR, textStatus, errorThrown) { $("#createformresults").text(errorThrown); }, //Options to tell jQuery not to process data or worry about content-type. cache: false, contentType: false, processData: false }); } else { //Fallback } return false; }); }); Copy FormData will support multi file upload! Add to your Form tag the attribute: enctype="multipart/form-data" NOTE: You may find that the $_FILES array is empty on server side page - In this case you need to make sure your server configuration allows file uploads, size limit of file upload is enough, post time is enough etc.... The best way to begin is to make sure that file uploads is allowed and then testing with very small files to be sure everything in your code is OK. Solution 2: Finally it is done!! Add this source to x.htmlBaca JugaError Getting Json Property In Vue JsHow Can I Create A File Upload Using Jquery And Php?What Is The Alternative Of Formdata In Ios? //Program a custom submit function for the form $("form#data").submit(function(event){ //disable the default form submission event.preventDefault(); //grab all form data var formData = new FormData($(this)[0]); $.ajax({ url: 'formprocessing.php', type: 'POST', data: formData, async: false, cache: false, contentType: false, processData: false, success: function (returndata) { alert(returndata); } }); return false; }); Copy <form id="data"> <input type="hidden" name="id" value="123" readonly="readonly"> User Name: <input type="text" name="username" value=""><br /> Profile Image: <input name="profileImg[]" type="file" /><br /> Display Image: <input name="displayImg[]" type="file" /><br /> <input type="submit" value="Submit"> </form> Copy And this to a PHP file (formprocessing.php): $id = $_POST['id']; $username = $_POST['username']; $profileImg = $_FILES['profileImg']; $displayImg = $_FILES['displayImg']; Copy I did it with the help this link http://digipiph.com/blog/submitting-multipartform-data-using-jquery-and-ajax Solution 3: ajax doesn't support file uploading at all. However, there are work-arounds. One of them is a jQuery plugin called Iframe Post Form, read more from: http://plugins.jquery.com/iframe-post-form/ $('form').iframePostForm({ complete : function (response) { $('#createarea').text("SUCCESS"); } }); Copy Apparently, you will have to specify the url action and enctype="multipart/form-data" in the form tag. Hope this helps! Share You may like these postsHow Do I Run A JQuery Function When Any Link (a) On My Site Is ClickedHow To Open A Hidden Div When A Hash Is On The URL?Input Field That Gets Taller As You TypeAngularJS Weird Render Issue Post a Comment for "Form Submit To Upload File And Also Other Fields Using Ajax"
Post a Comment for "Form Submit To Upload File And Also Other Fields Using Ajax"