javascript - Ajax multiple file upload dont work -
i'm usign code pass file input ajax. there 3 input file in html code .but first file input sent ajax. how fix code? mistake?
$("#add_order").click(function () { //*****get data input var formdata = new formdata(); formdata.append( 'action', 'add_order'); $.each($("input[type=file]"), function(i, obj) { $.each(obj.files,function(j,file){ alert(file.name); formdata.append('orderpic[]', file); }); }); //ajax start , ajax complatet ajax_loading(); $.ajax({ url: "includes/ajax/ajax.php", data: formdata, processdata: false, contenttype: false, type: 'post', datatype:'json', success: function(response){ //load json data server , output message if(response.type == 'error'){ //load json data server , output message output = '<div class="alert alert-danger" style="margin-top:12px;">'+response.text+'</div>'; }else{ output = '<div class="alert alert-success" style="margin-top:12px;">'+response.text+'</div>'; resetallvalues(); settimeout(function() { $('#new-order').modal('hide'); }, 1500); } $("#results").hide().html(output).fadein('slow').delay(800).fadeout(); }); }); html code:
<input type="file" name="orderpic[]" id="orderpic" class="form-control"> <input type="file" name="orderpic[]" id="orderpic" class="form-control"> <input type="file" name="orderpic[]" id="orderpic" class="form-control"> php code:
//file settings $files = array(); for($i=0; $i<count($_files['orderpic']['name']); $i++) { //get temp file path $tmpfilepath = $_files['orderpic']['tmp_name'][$i]; //make sure have filepath if ($tmpfilepath != "") { //setup our new file path $time = time(); $ext = pathinfo($_files['orderpic']['name'][$i], pathinfo_extension); $filepath = $uploaddir . $time .'.'. $ext; //upload file temp dir if (move_uploaded_file($tmpfilepath, $filepath)) { $resizeobj = new resize($filepath); $resizeobj -> resizeimage(200, 350, 'portrait'); $newfilepath = $uploaddir. "200_350_" .$time.'.'.$ext; $resizeobj -> saveimage($newfilepath, 100); unlink($filepath); $files[] = "200_350_" .$time.'.'.$ext; } } }
'id' cannot same input controls. change shown in html below , should work.
<input type="file" name="orderpic[]" id="orderpic1" class="form-control"> <input type="file" name="orderpic[]" id="orderpic2" class="form-control"> <input type="file" name="orderpic[]" id="orderpic3" class="form-control">
Comments
Post a Comment