【发布时间】:2019-02-18 11:28:00
【问题描述】:
我正在尝试在引导模式中填充表单字段,以使用 ajax 更新数据库中的数据。我没有得到任何数据,而不是带有空字段的模态弹出窗口。请检查我的代码并帮助我找出问题所在。
这里是控制器功能。
public function edit(){
$output = array();
$this->load->model("crud_data");
$patient_id = $this->input->post("patient_id");
$data = $this->crud_data->fetch_single($patient_id);
foreach ($data as $row) {
$output["name"] = $row->name;
$output["age"] = $row->age;
}
echo json_encode($output);
}
这里是 fetch_single 模态函数。
public function fetch_single($patient_id){
$this->db->where("patient_id", $patient_id);
$query = $this->db->get('patients');
return $query->result();
}
Ajax 的功能
$(document).on("click", '.edit', function(){
var patient_id = $(this).attr("patient_id");
$.ajax({
url:"<?php base_url(); ?>crud/edit",
method:"POST",
data:{'patient_id':patient_id},
dataType:"json",
success:function(data){
$("#edit").modal("show");
$("#name").val(data.name);
$("#age").val(data.age);
$("#patient_id").val(patient_id);
$(".modal-title").html("Edit User")
},
});
});
我想获取字段中的数据,以便更新它。
【问题讨论】:
-
称之为 $("#edit").modal("show");给模态赋值后
-
做了你建议的事情后没有运气
-
如果使用
console.log(data)会得到什么? -
使用
success:function(response){ console.log(response)}查看您的回复。使用inspect element时,您还可以在network选项卡中查看响应 -
您在弹出窗口中显示多条记录?
标签: javascript ajax codeigniter mysqli