jQuery dynamic Form, and the PHP $_FILES empty


I need to create dynamically an HTML form in jQuery, and when i click the submit button, then i send the form datas via Ajax to the 'add_sw.php' which is process them.

But my problem is that this PHP script cant access the PHP $_FILES variable, so that is empty..

Here is the js relevant js codes:

<script type="text/javascript"> $("body").on("click", "#submit", function() { var frm = $('#sw_add'); frm.submit(function (ev) { $.ajax({ type: frm.attr('method'), url: frm.attr('action'), data: frm.serialize(), success: function (data) { alert('ok'); } }); ev.preventDefault(); }); }); </script> <script type="text/javascript"> $("body").on("change", "select", function() { var tag = $("<div></div>"); var content = "<form id='sw_add' action='add_sw.php' method='post' enctype='multipart/form-data'>"; content += "<input type='text' name='sw_name'>"; content += "<label for='file'>Fájl:</label>"; content += "<input type='file' name='file' size='40'>"; content += "<textarea rows='4' cols='50' name='sw_comment'></textarea>"; content += "<div class='buttons'><button id='submit' type='submit'>Save</button></div>"; content += "</form>"; tag.html(content).dialog({title:'Add new software', modal:false, width:500, height:360}).dialog('open'); }); </script>

And the beginning the PHP script:

<?php $sw_name = $_REQUEST['sw_name']; $sw_file = $_FILES['file']['name']; $sw_comment = $_REQUEST['sw_comment']; // process the form datas... ?>

My problem is that the '$sw_file' variable is empty, i do not get the uploaded file name. Why?


Possible duplicate and check out this answer <a href="https://stackoverflow.com/questions/19295746/how-to-upload-multiple-files-in-php-using-jquery-and-ajax" rel="nofollow">How to upload multiple files using PHP, jQuery and AJAX</a> .


I modified the first js script, and this is a working version.

<script type="text/javascript"> $("body").on("click", "#submit", function() { $("#sw_add").submit(function(e) { var formObj = $(this); var formURL = formObj.attr("action"); var formData = new FormData(this); $.ajax({ url: formURL, type: 'POST', data: formData, mimeType: "multipart/form-data", contentType: false, cache: false, processData: false, success: function(data, textStatus, jqXHR) { alert('ok'); }, error: function(jqXHR, textStatus, errorThrown) { alert('error'); } }); e.preventDefault(); e.unbind(); }); }); </script>


  • How to get this simple one-way binding with knockout to work?
  • In J2ME, How I obtain reference of all controls of the form to record form's controls state cha
  • How to use three different keys e.g. (Ctrl Shift O) to open a form in vb.net?
  • Restoring deleted mysql database
  • Adding a Dropdown inside Kendo Grid
  • Is it possible to disable esc/F11 key during full screen mode of webpage, programmatically?
  • jquery code not working without breakpoint
  • Google OAuth: can't get refresh token with authorization code
  • How to plot large time series (thousands of administration times/doses of a medication)?
  • Android distinguish between tap and double tap
  • SAVE attribute needed for Fortran variables when only the C_LOC address is returned to a C program?
  • IE11 throwing “SCRIPT1014: invalid character” where all other browsers work
  • saving file generated by TCPDF
  • Adjust width of select element according to selected option's width
  • jQuery ready not fired after rails link_to is clicked
  • d3 v4 drag and drop with TypeScript
  • Django rest serializer Breaks when data exists
  • HTML download movie download link
  • AES padding and writing the ciphertext to a disk file
  • How to convert from System.Drawing.Color to Excel.ColorFormat in C#? Change comment color
  • Why doesn't :active or :focus work on text links in webkit? (safari & chrome)
  • Updating server-side rendering client-side
  • Validaiting emails with Net.Mail MailAddress
  • MySQL WHERE-condition in procedure ignored
  • Trying to switch camera back to front but getting exception
  • Web-crawler for facebook in python
  • Circular dependency while pushing http interceptor
  • AngularJs get employee from factory
  • trying to dynamically update Highchart column chart but series undefined
  • Free memory of cv::Mat loaded using FileStorage API
  • How to set the response of a form post action to a iframe source?
  • Angular 2 constructor injection vs direct access
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • Change div Background jquery
  • Programmatically clearing map cache
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal