我尝试使用post方法将值从ajax传递给codeigniter控制器,但它返回void

我尝试使用post方法将值从ajax传递给codeigniter控制器,但它返回void

问题描述:

I tried to store ajax value in codeigniter controller variable using post method I used $this->input->post method in the controller but the variable in the controller is not getting the ajax value, the variable is returning as null, help me to find a solution for this error Thanks in advance.

Here is the code:

View:

<button type="button" class="btn btn-info btn-md edit-pdt" data-pid="<?php echo $row->product_id;?>"><i class="fa fa-pencil"></i></button>

Controller:

public function displayprodt()
    {
        $pid= $this->input->get('pid');
        $data = array(
            'title' => "Edit Product",
            'result' => $this->ProductModel->displayprodt($pid)
        );
        $this->load->view('header',$data);
        $this->load->view('navbar');
        $this->load->view('sidebar');
        $this->load->view('editproduct',$data);
        $this->load->view('footer');
    }

JQuery:

$('.edit-pdt').click(function(){
        var base_url = $("#base_url").val();
        var pid = $(this).attr("data-pid");
        alert(pid);
        $.ajax({
           type: "GET",
           url: base_url+"index.php/Product/displayprodt",
           data: ({pid: pid}),
           success: function(response) {
             location.href = base_url+"index.php/product/displayprodt";
           }
        });
  });

Model:

public function displayprodt($pid){
        $this->db->select("*");
        $this->db->from("todaysdeal_products");
        $this->db->where("product_id",$pid);
        $query = $this->db->get();
        return $query->result();
    }
</div>

problem is your page redirects whatever returned from first request. please try replacing location.href = base_url+"index.php/product/displayprodt"; with console.log(response); or alert(response);

You are using $this->input->post('pid') then you have to use POST in ajax.
change

1: type: "GET", to type: "POST",
2: data: ({pid: pid}), to data: {pid: pid},

i think you need to create controller to control ajax data i will give you example :

Controller

public function displayprodt()
{
      $pid = $this->input->post('pid',true);

           $data=array(
                'error' =>false,
                'title' => 'Edit Product',
                'result' => $this->ProductModel->displayprodt($pid),
            );


      header('Content-Type: application/json');
      echo json_encode($data ,JSON_PRETTY_PRINT);
}

Model

public function displayprodt($pid){
        $this->db->select("*");
        $this->db->from("todaysdeal_products");
        $this->db->where("product_id",$pid);
        $query = $this->db->get();
        return $query->result();
    }

Jquery

$('.edit-pdt').click(function(){
        var base_url = $("#base_url").val();
        var pid = $(this).attr("data-pid");
        alert(pid);
        $.ajax({
           type: "POST",
           url: base_url+"index.php/Api/displayprodt",
           data: ({pid: pid}),
           dataType: "JSON",
           success: function(response) {
             location.href = base_url+"index.php/product/displayprodt"; // or any url you want redirect.
           }
        });
  });

I hope you find solution :')