My form in view is
<?php
$attributes = array('id' => 'myForm');
echo form_open_multipart('', $attributes); ?>
<div class="form-group">
<label for="image">Main Image</label>
<input type="file" class="form-control-file" id="image" name="image" aria-describedby="fileHelp">
</div>
<div style="text-align:center">
<button type="button" class="btn btn-primary" id="add">ADD</button>
<button type="reset" class="btn btn-primary">Reset</button>
</div>
<?php echo form_close(); ?>
And my script is as follows
<script>
$("#add").click(function () {
var image = $("#image").val() ;
$.ajax({
type: "POST",
url: "<?php echo site_url('form/addArticleData'); ?>",
data: {
image: image,
},
dataType: "html",
contentType: false,
cache: false,
processData: false,
success: function(data) {
if(data) {
alert($.trim(data));
$("#myForm")[0].reset();
} else {
alert("Some Error Occurred. Please Try Again!!!");
}
}, error: function() {
alert("ERROR!");
}
});
});
</script>
And my addArticleData function inside the controller form contains
if(isset($_FILES["image"]["name"]))
{
$config = array(
'upload_path' => "images",
'allowed_types' => "*",
'overwrite' => TRUE,
'max_size' => "2048000", // Can be set to particular file size , here it is 2 MB(2048 Kb)
'max_height' => "768",
'max_width' => "1024"
);
$this->load->library('upload', $config);
if($this->upload->do_upload('image'))
{
$data = array(
'image' => $this->input->post('image')
);
//print_r($data);
if($this->modal->insert($data, 'admin')) {
echo "DATA INSERTED SUCCESSFULLY";
}
}
else {
echo "ERROR";
}
} else {
echo "FILE NOT SET";
}
The problem is that whenever I tried to execute this script it alerts FILE NOT SET. And if I print $_FILES using print_r($_FILES);exit; it alerts an empty array.
Further if I try commented print_r($data);exit; and remove all the lines preceding it then it alerts the name of the image uploaded as C:\fakepath\image.png
I just can't understand why this is not working. Please HELP