html - Uploading files with AJAX to PHP -
i'm seding data - including files php, if throuh html , post everyting works alright, not while trying upload ajax , wonder why.
$scope.aceptar = function() { var form = $('form'); var formdata = new formdata(form); var file_1 = document.getelementbyid('file_1').files[0]; var file_2 = document.getelementbyid('file_2').files[0]; var file_3 = document.getelementbyid('file_3').files[0]; formdata.append(file_1.name, file_1); formdata.append(file_2.name, file_2); formdata.append(file_3.name, file_3); $.ajax({ method: "post", url: "http:dsfs43r4.php", data: formdata, processdata: false, async: false, success: function(data) { console.log(data); }, error: function(data) { alert("error"); } }); }
and in php echo $_files i'm getting
array(0) {}
as answer on chrome console.
edit:
if do:
<form id="theform" enctype="multipart/form-data" action="http://ge/_m.php" method="post"> <button type="submit" ng-click="aceptar()">aceptar</button> </form>
i $_post - i'm expecting:
array(15) { ["lote"]=> string(8) "tyrrtyrw" ["kg__muestra"]=> string(0) "" ["kg__ceb__aprovechable"]=> string(0) "" ["-/50_mm"]=> string(0) "" ["50/60_mm"]=> string(0) "" ["60/75_mm"]=> string(0) "" ["75/90_mm"]=> string(0) "" ["90/+_mm"]=> string(0) "" ["kg__destrío"]=> string(0) "" ["kg__podridos"]=> string(0) "" ["kg__merma"]=> string(0) "" ["kg__podrido_interno"]=> string(0) "" ["kg__germinación_int__1/3"]=> string(0) "" ["kg__germinación_int__2/3"]=> string(0) "" ["cantidad_en_kg_"]=> string(0) "" } hubo un problema al subir los ficheros.
so idk why doing through ajax wouldnt give me same result.
here's did enable ajax form uploads, tweak suit , should job you! element file in example #evidence
var jform = new formdata(); jform.append('supply_id',supply_id); jform.append('fuel_usage',$('#fuel_usage').val()); jform.append('cost',$('#cost').val()); jform.append('currency',$('#currency').val()); jform.append('reading',$('#reading').val()); jform.append('data_source',$('#data_source').val()); jform.append('date_from',$('#date_from').val()); jform.append('date_to',$('#date_to').val()); jform.append('evidence',$('#evidence').get(0).files[0]); jform.append('comments',$('#comments').val()); $.ajax({ url: '/your-form-processing-page-url-here', type: 'post', data: jform, datatype: 'json', mimetype: 'multipart/form-data', contenttype: false, cache: false, processdata: false, success: function(data, status, jqxhr){ alert('hooray! well.'); console.log(data); console.log(status); console.log(jqxhr); }, error: function(jqxhr,status,error){ // should never reach here console.log(jqxhr); console.log(status); console.log(error); } });
Comments
Post a Comment