ajax 不会进入控制器并且数据不会更新
ajax is not going to controller and data is not updating
我的 Ajax 脚本不会进入控制器,数据也不会上传。
脚本是:
$("#update").click(function(event) {
/* Act on the event */
var chango = $("#update_form").serialize();
alert(chango);
$.ajax({
type:"POST",
url:"<?php echo base_url() ?>home/update_profile",
data:{id:id}
},
function(data) {
console.log(data);
list_user();
});
event.preventDefault();
});
我的控制器是:
public function update_profile()
{
try {
$this->load->library('form_validation');
$this->form_validation->set_rules("fname", "First Name", 'required|alpha');
$this->form_validation->set_rules("lname", "Last Name", 'required|alpha');
$this->form_validation->set_rules("mobile", "Mobile", 'required');
$this->form_validation->set_rules("dob", "Date of Birth", 'required');
$this->form_validation->set_rules("gender", "Gender", 'required');
if ($this->form_validation->run()){
$this->load->model("user");
$data=array(
'fname' => $this->input->post('fname'),
'lname' => $this->input->post('lname'),
'mobile' => $this->input->post('mobile'),
'dob' => $this->input->post('dob'),
'gender' => $this->input->post('gender')
);
if ($this->input->post("update")) {
$this->user->update_data($data, $this->input->post("id"));
redirect(base_url() . "home/list_user");
}
}
else
{
}
}
catch(Exception $e) {
}
$this->load->view('middlepage/update_profile.php');
}
public function update()
{
$user_id = $this->uri->segment(3);
$this->load->model("user");
$data["user_data"] = $this->user->fetch_single_data($user_id);
$data["fetch_data"] = $this->user->fetch_data1();
$this->load->view("middlepage/update_profile", $data);
}
我的模型是:
public function fetch_single_data($id)
{
$this->db->where("id", $id);
$query = $this->db->get("user");
return $query;
}
public function update_data($data, $id)
{
$this->db->where("id", $id);
return $this->db->update("user", $data);
}
我的观点是:
<form method="post" id="update_form" enctype="multipart/form-data" novalidate="true">
<?php if(isset($user_data))
{
foreach ($user_data->result() as $row) {
?>
<div class="form-group valid-form">
<h4>First Name:</h4>
<input type="text" class="form-control" id="fname" name="fname" value="<?php echo $row->fname ; ?>">
<span class="text-danger"><?php echo form_error("fname");?></span>
</div><br>
<div class="form-group valid-form">
<h4>Last Name:</h4>
<input type="text" class="form-control" id="lname" name="lname" value="<?php echo $row->lname ; ?>">
<span class="text-danger"><?php echo form_error("lname");?></span>
</div><br>
<div class="form-group valid-form">
<h4>Mobile:</h4>
<input type="text" class="form-control" id="mobile" name="mobile" value="<?php echo $row->mobile ; ?>">
<span class="text-danger"><?php echo form_error("mobile");?></span>
</div><br>
<div class="form-group valid-form">
<h4>Date of Birth:</h4>
<input type="date" class="form-control" min="1984-01-01" max="2005-12-31" id="dob" name="dob" value="<?php echo $row->dob ; ?>">
<span class="text-danger"><?php echo form_error("dob");?></span>
</div><br>
<div class="form-group">
<h4>Gender</h4>
<div class="radio">
<label>
<input type="radio" name="gender" id="male" value="male"<?php if($row->gender == 'male') echo "checked"; ?>><span style="font-weight: bolder; font-size: 15px;">Male</span>
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="gender" id="female" value="female"<?php if($row->gender == 'female') echo "checked"; ?>><span style="font-weight: bolder; font-size: 15px;">Female</span>
</label>
</div>
<span class="text-danger"><?php echo form_error("gender");?></span>
</div><br>
<div class="form-group">
<input type="hidden" name="id" value="<?php echo $row->id ?>">
<input type="submit" name="update" id="update" value="Update" class="btn btn-primary">
</div>
<?php
}
}
?>
</form>
我无法更新数据库中的数据,也不会列出 table
ajax 功能不工作,不会进入 codeigniter 的控制器
虽然尝试使用这种类型的类似代码来提交,但效果很好
从哪里获取您作为数据传递到 id 字段中的 id
$.ajax({
type:"POST",
url:"<?php echo base_url() ?>home/update_profile",
data:{id:id} <--
},
我的 Ajax 脚本不会进入控制器,数据也不会上传。
脚本是:
$("#update").click(function(event) {
/* Act on the event */
var chango = $("#update_form").serialize();
alert(chango);
$.ajax({
type:"POST",
url:"<?php echo base_url() ?>home/update_profile",
data:{id:id}
},
function(data) {
console.log(data);
list_user();
});
event.preventDefault();
});
我的控制器是:
public function update_profile()
{
try {
$this->load->library('form_validation');
$this->form_validation->set_rules("fname", "First Name", 'required|alpha');
$this->form_validation->set_rules("lname", "Last Name", 'required|alpha');
$this->form_validation->set_rules("mobile", "Mobile", 'required');
$this->form_validation->set_rules("dob", "Date of Birth", 'required');
$this->form_validation->set_rules("gender", "Gender", 'required');
if ($this->form_validation->run()){
$this->load->model("user");
$data=array(
'fname' => $this->input->post('fname'),
'lname' => $this->input->post('lname'),
'mobile' => $this->input->post('mobile'),
'dob' => $this->input->post('dob'),
'gender' => $this->input->post('gender')
);
if ($this->input->post("update")) {
$this->user->update_data($data, $this->input->post("id"));
redirect(base_url() . "home/list_user");
}
}
else
{
}
}
catch(Exception $e) {
}
$this->load->view('middlepage/update_profile.php');
}
public function update()
{
$user_id = $this->uri->segment(3);
$this->load->model("user");
$data["user_data"] = $this->user->fetch_single_data($user_id);
$data["fetch_data"] = $this->user->fetch_data1();
$this->load->view("middlepage/update_profile", $data);
}
我的模型是:
public function fetch_single_data($id)
{
$this->db->where("id", $id);
$query = $this->db->get("user");
return $query;
}
public function update_data($data, $id)
{
$this->db->where("id", $id);
return $this->db->update("user", $data);
}
我的观点是:
<form method="post" id="update_form" enctype="multipart/form-data" novalidate="true">
<?php if(isset($user_data))
{
foreach ($user_data->result() as $row) {
?>
<div class="form-group valid-form">
<h4>First Name:</h4>
<input type="text" class="form-control" id="fname" name="fname" value="<?php echo $row->fname ; ?>">
<span class="text-danger"><?php echo form_error("fname");?></span>
</div><br>
<div class="form-group valid-form">
<h4>Last Name:</h4>
<input type="text" class="form-control" id="lname" name="lname" value="<?php echo $row->lname ; ?>">
<span class="text-danger"><?php echo form_error("lname");?></span>
</div><br>
<div class="form-group valid-form">
<h4>Mobile:</h4>
<input type="text" class="form-control" id="mobile" name="mobile" value="<?php echo $row->mobile ; ?>">
<span class="text-danger"><?php echo form_error("mobile");?></span>
</div><br>
<div class="form-group valid-form">
<h4>Date of Birth:</h4>
<input type="date" class="form-control" min="1984-01-01" max="2005-12-31" id="dob" name="dob" value="<?php echo $row->dob ; ?>">
<span class="text-danger"><?php echo form_error("dob");?></span>
</div><br>
<div class="form-group">
<h4>Gender</h4>
<div class="radio">
<label>
<input type="radio" name="gender" id="male" value="male"<?php if($row->gender == 'male') echo "checked"; ?>><span style="font-weight: bolder; font-size: 15px;">Male</span>
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="gender" id="female" value="female"<?php if($row->gender == 'female') echo "checked"; ?>><span style="font-weight: bolder; font-size: 15px;">Female</span>
</label>
</div>
<span class="text-danger"><?php echo form_error("gender");?></span>
</div><br>
<div class="form-group">
<input type="hidden" name="id" value="<?php echo $row->id ?>">
<input type="submit" name="update" id="update" value="Update" class="btn btn-primary">
</div>
<?php
}
}
?>
</form>
我无法更新数据库中的数据,也不会列出 table ajax 功能不工作,不会进入 codeigniter 的控制器
虽然尝试使用这种类型的类似代码来提交,但效果很好
从哪里获取您作为数据传递到 id 字段中的 id
$.ajax({
type:"POST",
url:"<?php echo base_url() ?>home/update_profile",
data:{id:id} <--
},