我尝试通过ajax jQuery在codeigniter框架中提交表单数据,但总是无法避免页面刷新并且出现“失败”信息。
我对ajax不是很熟悉,请帮我解决这个问题。
这是我的控制器:
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);
}
以下是我的观点:
<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>
以下是JavaScript代码:
$(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');
}
});
});
});
获取值的模型:
public function get_jsid($id) {
$sql = "SELECT jobseeker_id FROM tbl_jobseeker WHERE tbl_user_u_id = ".$id.";";
return $this->db->query($sql)->row();
}
url: 'http://localhost/joblk.com/index.php/jobseeker/add_personal'
中没有传递jobseeker_id
。 - Abdulla Nilamconsole.log(data);
添加到成功和错误函数中以查看浏览器控制台,并按照Abdulla的建议更改错误函数。 - Sanjuktha