I'm unable to upload the image by ajax,it always redirect to the page whenever I click submit button and the data is not added to the database as well.
form
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>" id="ImageUploadForm" enctype="multipart/form-data">
<input type="text" name="caption" id="caption">
<input type="file" name="image" id="ImageBrowse"/>
<input type="submit" class="btn btn-success" value="Save" />
</form>
ajax
$(document).ready(function (e) {
$("#imageUploadForm").on('submit',(function(e) {
e.preventDefault();
$.ajax({
url: "<?php echo base_url();?>adminpromisess/addgal",
type: "POST",
data: new FormData(this),
contentType: false,
cache: false,
processData:false,
success: function(data) {
alert(data);
//$("#gallery-form").load(".gallery-form");
},
error: function() {
}
});
}));
});
add data to database function (controller)
public function addgal(){
$caption = $this->input->post('caption');
$promises = 00;
$description = $this->input->post('description');
$image = $_FILES['image']['name'];
if(move_uploaded_file($_FILES['image']['tmp_name'], 'assets/img/upload/promises_image/'.$image)){
$data = array(
'caption' => $caption,
'promises' => $promises,
'gal_desc' => $description,
'image' => $image
);
$result = $this->adminpromisesmodel->addGallery($data);
}else{
echo "Fail to upload file";
die();
}
}
note:model (query) to save the database is correct so i didn't post it