Javascript Codeigniter:使用jQueryAjax提交表单数据而不刷新页面
我曾尝试使用AJAXjQuery在codeigniter框架中提交表单数据而不刷新页面,但它总是传递到Javascript Codeigniter:使用jQueryAjax提交表单数据而不刷新页面,javascript,php,jquery,ajax,codeigniter,Javascript,Php,Jquery,Ajax,Codeigniter,我曾尝试使用AJAXjQuery在codeigniter框架中提交表单数据而不刷新页面,但它总是传递到fail消息 我是ajax新手,请帮助我解决此错误 这是我的控制器: public function add_personal() { $id = $this->uri->segment(3); $jid = $this->Jsprofile->get_jsid($id)->jobseeker_id; $data = array(
fail
消息
我是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>
public function get_jsid($id) {
$sql = "SELECT jobseeker_id FROM tbl_jobseeker WHERE tbl_user_u_id = ".$id.";";
return $this->db->query($sql)->row();
}
以下是我的观点:
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>
public function get_jsid($id) {
$sql = "SELECT jobseeker_id FROM tbl_jobseeker WHERE tbl_user_u_id = ".$id.";";
return $this->db->query($sql)->row();
}
获取值的模型:
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>
public function get_jsid($id) {
$sql = "SELECT jobseeker_id FROM tbl_jobseeker WHERE tbl_user_u_id = ".$id.";";
return $this->db->query($sql)->row();
}
AJAX中的
<script>
$(document).ready(function(){
$("#personal-info").submit(function(e){
e.preventDefault();
var title = $("#js_personal_title").val();;
var decs= $("#js_personal_desc").val();
$.ajax({
type: "POST",
url: '<?php echo base_url() ?>index.php/jobseeker/add_personal',
data: {title:title,decs:decs},
success:function(data)
{
alert('SUCCESS!!');
},
error:function()
{
alert('fail');
}
});
});
});
</script>
在控制器中
function add_personal()
{
$id = $this->uri->segment(3);
//im confusing this part
$jid = $this->Jsprofile->get_jsid($id);
$data = array(
'js_personal_title' => $this->input->post('title'),
'js_personal_desc' => $this->input->post('decs'),
'tbl_jobseeker_jobseeker_id' => $jid[0]['jobseeker_id'],
'tbl_jobseeker_tbl_user_u_id'=>$id
);
// echo json_encode($data);
$this->db->insert('tbl_js_personal',$data);
}
在型号中
function get_jsid($id)
{
$query = $this->db->query("SELECT jobseeker_id FROM tbl_jobseeker WHERE tbl_user_u_id = '$id'");
$result = $query->result_array();
return $result;
}
在config/autoload.php中
$autoload['libraries'] = array('database');
试试这个:
查看
<form action="" method="POST" id="personal-info" class="form-group">
<input class="form-control" id="uri_segment_id" type="hidden" value="<?php echo $this->uri->segment(3); ?>">
<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>
请检查您的浏览器控制台
是否有错误。您的ajax urlurl:'http://localhost/joblk.com/index.php/jobseeker/add_personal“,
@Abdulla仍然需要使用console.log()检查错误;在firebug控制台或浏览器控制台console.log(数据)中查找错误或响应
将其添加到success和error函数中,以查看浏览器控制台,并按照Abdulla所述更改error函数。没有URI段是3,因为CI忽略了index.php
,我已经对此进行了回显和检查。我仍然使用此$jid=$this->Jsprofile->get_jsid($id)做了相同的回答->求职者身份证代码>MySQL列的值tbl_jobsequeer_jobsequeer_id
,我用Echor检查过,你检查过模型工作吗??cz型号名称应为Jsprofile\u型号
否,URL中没有更改您是否尝试过$(“#提交-p”)。以这种方式单击(函数(e)
?如果您发现错误,请更新是什么,好奇地想知道问题出在哪里。