【问题标题】:Ajax not getting data from database in codeigniterAjax没有从codeigniter中的数据库获取数据
【发布时间】: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


【解决方案1】:
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);
    $output["name"] = $data ->name;
    $output["age"]  = $data ->age;
    $this->output->set_content_type('application/json');
    $this->output->set_output(json_encode($output));
}

如果您的模型和数据库正确完成工作,则此代码必须正确。

【讨论】:

  • 仍然没有运气...在控制台中获取空数组
  • 邮递员直接邮寄到您的控制器会得到什么? localhost/ci_project/patients/edit/ 和数据字段 patient_id = 1 ?
  • 那里也有 [ ] 空数组
  • 检查您的数据库是否有任何数据。那么如果在您的模型中使用以下代码$this-&gt;db-&gt;select() -&gt;from('patients') -&gt;where("patient_id", $patient_id); $query = $this-&gt;db-&gt;get();
【解决方案2】:

在成功的 ajax 中,当您从控制器发送 json_encode 时,您没有解析数据。 所以解析json_encode成功,然后将数据添加到modal中。

 var parse_data = JSON.parse(data); 

谢谢

【讨论】:

    【解决方案3】:

    控制器

    如果你只得到一行,那么不要使用循环

    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);
    $output["name"] = $data ->name;
    $output["age"]  = $data ->age;
    echo json_encode($output);
    }
    

    型号

    要仅获取一行,请使用 row() 而不是 result()

    public function fetch_single($patient_id){
      $this->db->where("patient_id", $patient_id);
      $query = $this->db->get('patients');
      return $query->row();
    }
    

    Ajax 成功函数

    为输入字段赋值后加载模态

    success:function(data){ 
      $("#name").val(data.name);
      $("#age").val(data.age);
      $("#patient_id").val(patient_id); 
      $(".modal-title").html("Edit User");
      $("#edit").modal("show");
    }
    

    【讨论】:

      【解决方案4】:

      我不能告诉你你错了什么,但它很容易调试。首先,您可以控制台在ajax 中接收到的数据。在成功函数中,您可以控制台数据。如果它为空,您将检查您的 php 函数。检查输入 $patient_id isset,并检查 $this->crud_data->fetch_single($patient_id); 的 put

      简而言之,你应该一步一步检查你的错误

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-27
        • 1970-01-01
        • 1970-01-01
        • 2017-03-04
        • 2016-10-29
        • 1970-01-01
        • 2016-12-16
        相关资源
        最近更新 更多