I have tried to submit a form data in codeigniter framework with ajax jQuery without page refreshing but it always pass to fail message.
I'm new to ajax, help me to solve this error.
Here is my Controller:
public function add_personal() {
$id = $this->uri->segment(3);
$jid = $this->Jsprofile->get_jsid($id)->jobseeker_id;
$data = array(
'js_personal_title' => $this->input->post('js_personal_title'),
'js_personal_desc' => $this->input->post('js_personal_desc'),
'tbl_jobseeker_jobseeker_id' => $jid,
'tbl_jobseeker_tbl_user_u_id'=>$id
);
// echo json_encode($data);
$this->load->database();
$this->db->insert('tbl_js_personal',$data);
}
Here is my view:
<form action="" method="POST" id="personal-info" class="form-group">
<input class="form-control" type="text" name="js_personal_title">
<input class="form-control" type="text" name="js_personal_desc">
<input id="submit-p" class="form-control" type="submit" value="Add">
</form>
Here is js code :-
$(document).ready(function(){
$("#personal-info").submit(function(e){
e.preventDefault();
var data= $("#personal-info").serializeArray();
$.ajax({
type: "POST",
url: 'http://localhost/joblk.com/index.php/jobseeker/add_personal',
data: data,
success:function(data) {
alert('SUCCESS!!');
},
error: function (XHR, status, response) {
alert('fail');
}
});
});
});
Model for get values:
public function get_jsid($id) {
$sql = "SELECT jobseeker_id FROM tbl_jobseeker WHERE tbl_user_u_id = ".$id.";";
return $this->db->query($sql)->row();
}
browser's consolefor errors.jobseeker_idin your ajax urlurl: 'http://localhost/joblk.com/index.php/jobseeker/add_personal',console.log(data);add this to both success and error function to see the browser console, and also change the error function as Abdulla mentioned above.