Image upload in CI with Ajax
Controlar==>
public function Register(){
$data['main_content'] = 'ajax';
$this->load->view('partials/header',$data);
$this->load->view('ajax', $data);
$this->load->view('partials/footer',$data);
}
public function register_post(){
$target_dir =dirname(dirname(__dir__)) . "/uploads/";
$target_file = $target_dir . basename($_FILES["image"]["name"]);
$filetmp_name = $_FILES["image"]["tmp_name"];
if(move_uploaded_file($filetmp_name, $target_file)) {
$set=array(
'data'=>'uploooo',
'status'=>'1',
);
echo json_encode($set);
}else{
$set=array(
'data'=>'image is not uploded',
'status'=>'0',
);
echo json_encode($set);
}
}
View ===>
<form action="<?=base_url() ;?>register-post" id="fomregister" method="post" enctype="multipart/form-data">
<input type="text" name="name" placeholder="Name" /> </br>
<input type="text" name="email" placeholder="email" /> <br>
<input name="image" type="file" /> <br>
<button type="submit">Submit</button>
</form>
<script>
$(document).on('submit','#fomregister',function(e) {
e.preventDefault();
var formData = new FormData(this);
$.ajax({
type: 'POST',
url:'<?=base_url();?>register-post',
data: formData,
success: function (data) {
alert(data);
},
cache: false,
contentType: false,
processData: false,
});
});
</script>
Comments
Post a Comment