【问题标题】:Get data from ajax and pass the data to view in CodeIgniter 3从 ajax 获取数据并将数据传递到 CodeIgniter 3 中查看
【发布时间】:2019-10-05 13:35:16
【问题描述】:

我正在为一家酒店开发一个管理网站。

我在员工表的每一行都有一个编辑按钮(它的图标标签)。单击编辑按钮时,它将通过 ajax 将数据发送到另一个站点的表单。但是如果我加载editBooking的视图,浏览器会返回ajax错误

$this->load->view('Booking/viewBookingView').

我的 js:

$(".editBooking").click((event)=>{
          var table_data = [];
          var $row_data = $(event.target).parent().parent();

          vdata = {
            'name': $($row_data[0]).find('td:eq(0)').text(),
            'arrivalDate': $($row_data[0]).find('td:eq(1)').text(),
            'departureDate': $($row_data[0]).find('td:eq(2)').text(),
            'roomType': $($row_data[0]).find('td:eq(3)').text(),
            'quantity': $($row_data[0]).find('td:eq(4)').text()
          };// get data of row

          table_data.push(vdata);
          var data = { 'data_table' : table_data};
          console.log(data)
          $.ajax({
            data: data,
            type: 'GET',
            url: '<?php echo base_url('index.php/AdminBooking/viewBooking/edit')?>',
            crossOrigin: false,
            dataType: 'json',
            error: ()=>{
              alert('ajax failed');
            },
            success: (result)=>{
              alert('ajax success');
              console.log(result.check)
            }
          })
        })

我的 php 控制器:

<?php
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Methods: GET,POST, OPTIONS");

class ViewBooking extends CI_Controller{

    public function index(){

        $this->load->helper('html');
        $this->load->helper('url');
        $this->load->view('Booking/viewBookingView');
    }

    public function edit(){
        $ajaxData = $this->input->post('data_table');

        $data = array(
            'name' => $ajaxData[0]['name']
        );

        $this->load->view('Booking/test.php',$data);
        $this->output->set_content_type('application/json');
        echo json_encode(array(
            'check' => $data['name']
            ));//Kiem tra json  hoat dong khong?
    }
}

我希望当我单击编辑按钮时,编辑页面将加载表格中的数据。

【问题讨论】:

    标签: php ajax codeigniter-3


    【解决方案1】:

    在您的情况下,您必须将单击的值存储在会话或任何其他方式中,然后在函数中调用该值,该函数将从 db 表中获取信息,然后在下一个视图中返回函数结果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-27
      • 1970-01-01
      • 1970-01-01
      • 2016-03-08
      • 1970-01-01
      • 2018-05-09
      相关资源
      最近更新 更多