Currently I am uploading image in codeigniter via ajax. In this all my input fields are taken, but input file was not getting. I am getting error undefine "image name" how to do image upload via ajax. I have attached my code below,
HTML:
<form method="post" enctype="multipart/form-data" action="" id="newsFeedForm">
<input type="file" name="newsimages<?php echo $i; ?>[]" id="newsimages<?php echo $i; ?>" data-feed="feed<?php echo $i; ?>" multiple>
<input type="button" id="newsSubmit" value="Update" class="btn btn-primary btn-sm pull-right mt10 col-lg-2 col-sm-2 col-xs-2 mt10" name="newsSubmit" onclick="updateFeedDatabase(<?php echo $i.','.$fed->news_id; ?>);">
</form>
ajax:
function updateFeedDatabase(id, nid){
var myData = $('#your_form').serialize();
//alert(formData);
$.ajax({
type:"POST",
contentType:attr( "enctype", "multipart/form-data" ),
url:"<?=base_url();?>nhome/updateFeed/"+id+"/"+nid,
data: myData,
cache: false,
processData: false,
success: function (data) {
alert(data);
},
error: function(data){
//error function
}
});
}
Model:
public function updatefeedDb($id,$nid){
$message = $this->input->post('newsMessage'.$id);
$status = $this->input->post('newsstatus'.$id);
echo $_FILES['newsimages'.$id]['name'];
if (empty($_FILES['newsimages'.$id]['name'])) {
echo "I am not empty";
}else{
echo "I am empty";
}
}
In this above code, I am getting undefined "newsimages1". How to get image in to ajax page, Can you please help in this task.