【问题标题】:How can I get data from database using ajax in php codeigniter and display it in modal?如何在 php codeigniter 中使用 ajax 从数据库中获取数据并以模式显示?
【发布时间】:2017-05-08 01:21:09
【问题描述】:

我只想动态地从数据库中获取数据,所以我使用了 onclick ajax 方法从数据库中获取数据而无需重新加载。我还想在 Modal CSS 中显示它。

<td><a style="cursor:pointer;" data-toggle="modal" data-target="#myModal" onclick="get_report(<?php echo $det->user_id; ?>);">Today Report</a>
                            <div class="modal fade" id="myModal" role="dialog">
                                <div class="modal-dialog">
                                    <div class="modal-content">
                                        <div class="modal-header">
                                            <button type="button" class="close" data-dismiss="modal">&times;</button>
                                            <h4 class="modal-title">Today Report</h4>
                                        </div>
                                        <div class="modal-body">
                                            <div id="rep_id"></div>
                                        </div>
                                        <div class="modal-footer">
                                            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </td>

在底部,

function get_report(sales_id)
{ 
    $.ajax({
            url: "<?php echo base_url()."Sales_exec/get_report/"; ?>",
            method: "post",
            data : { sales_id : sales_id }
        }).done(function(response){ 
               $('.modal-body').html(response);
        }); 
}
</script>

在我的控制器中,

public function get_report()
{
    $sales_id = $this->input->post('sales_id'); 
    $response = $this->Sales_exec_model->today_sales_report($sales_id);
}

我的模型是,

public function today_sales_report($id)
    {
        $query = $this->db->query('SELECT * FROM tbl_reports WHERE sales_id="'.$id.'" AND DATE(created) = CURDATE()');
        if ($query->num_rows() == 1)
        {
            $report['rep']= $query->row();
            print_r($report);die;
        }
    }

我在模态的正文中得到输出:

数组 ( [rep] => 标准类对象 ( [rep_id] => 3 [client_name] => Joyner [地址] => client_address [area] => client_area [sales_id] => 1 [sales_head_id] => 85 [comment] => 0 [pay_by] => 现金 [amt_value] => 1.00 [创建] => 2016-12-22 ) )

谁能帮我把这些数据从数组中分离出来显示出来?

【问题讨论】:

  • 使用$report['rep'] = $query-&gt;row_array(); 返回一个数组。如果要返回 HTML,请调用视图。 Otherwise return json.
  • 如果我调用视图然后页面将被重新加载以显示来自数据库的数据。我想通过单击链接将数据库中的数据显示到 CSS Modal 中。
  • 视图只会将 html 回显到您的 ajax 调用,因此不会重新加载页面。如果你想you can call a view so it returns the output to a variable 然后返回。 $report['rep'] = $query-&gt;row_array(); $result = $this-&gt;load-&gt;view('myView', $report, TRUE); echo $result;

标签: javascript php jquery ajax codeigniter


【解决方案1】:

将模式 $report['rep'] 更改为 $report。因为您将值保存在 $report['rep'] 并尝试从 $report 获取值。 所以改变你的模式和 将您的 $report['rep'] 更改为 $report 这将解决您的问题。

【讨论】:

  • 没有改变我得到相同的输出.. 我不知道如何调用该数组元素将其打印为正确的数据
猜你喜欢
  • 2013-01-26
  • 2023-03-27
  • 1970-01-01
  • 2018-07-26
  • 1970-01-01
  • 1970-01-01
  • 2019-04-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多