我的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>
我无法更新数据库中的数据,也不会列出表 ajax功能无法正常工作,并且无法转到codeigniter的控制器
虽然正在尝试使用这种类似的代码来提交,但效果很好
答案 0 :(得分:0)
您从哪里获得ID,并在ID字段中作为数据传递
$.ajax({
type:"POST",
url:"<?php echo base_url() ?>home/update_profile",
data:{id:id} <--
},