php - submit form via jquery FormData -
i trying upload file using jquery. here's form:
<form id="upload_data" class="project" enctype="multipart/form-data" method="post"> <select id="project_id" name="project_id"> <option value="1">1</option> <option value="2">2</option> </select> <input type="file" id="data" name="data" /> <input type="hidden" name="user_id" id="user_id" value="12" required> <button type="submit" id="ajax" class="btn btn-primary">submit</button> </form>
here's jquery part:
$('#upload_data').submit( function( e ) { var form = $('form'); var formdata = new formdata(); $.each($(':input', form ), function(i, fileds){ formdata.append($(fileds).attr('name'), $(fileds).val()); }); $.each($('input[type=file]',form )[0].files, function (i, file) { formdata.append(file.name, file); }); $.ajax( { url: '../controllers/process.php', type: 'post', enctype: 'multipart/form-data', data: formdata, processdata: false, contenttype: false, success: function(data) { alert(data); }, error: function(data) { alert(data); } } ); });
the problem getting empty post values in process.php:
if (isset($_post['project_id']) , isset($_post['user_id'])){ //process }else{ echo 'no post data'; }
it return 'no post data' (but in console, can see data has been posted it's not case in server.). missing here?
you can use ajaxform/ajaxsubmit functions ajax form plugin or jquery serialize function.
and
Comments
Post a Comment