简体   繁体   English

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

[英]I tried to pass value from ajax to codeigniter controller using post method but it is returning 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. 我试图使用post方法在codeigniter控制器变量中存储ajax值,我在控制器中使用了$ this-> input-> post方法,但是控制器中的变量未获取ajax值,该变量返回为null,请帮助我为该错误找到解决方案预先感谢。

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: 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();
    }

You are using $this->input->post('pid') then you have to use POST in ajax. 您正在使用$this->input->post('pid')那么您必须在ajax中使用POST。
change 更改

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

problem is your page redirects whatever returned from first request. 问题是您的页面重定向了第一个请求返回的内容。 please try replacing location.href = base_url+"index.php/product/displayprodt"; 请尝试替换location.href = base_url+"index.php/product/displayprodt"; with console.log(response); console.log(response); or alert(response); alert(response);

i think you need to create controller to control ajax data i will give you example : 我认为您需要创建控制器来控制Ajax数据,我将举一个例子:

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 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 :') 希望您能找到解决方法:')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么当我尝试从codeigniter中的控制器文件返回数据时,ajax post方法返回空警报框 - why ajax post method return empty alert box when i tried to return data from controller file in codeigniter 无法使用Ajax将值传递给Codeigniter控制器 - Cannot pass value to Codeigniter controller using ajax Ajax发布不从cakephp控制器返回值 - Ajax post not returning value from cakephp controller 我想使用ajax post从视图传递codeigniter中的数据,在控制器中处理该数据,然后将结果数组发送回视图 - I want to pass data in codeigniter from view using ajax post, process that data in controller and send back the result array to view AJAX jQUERY将POST中的数据传递给codeigniter控制器 - AJAX jQUERY to pass data in POST to codeigniter Controller 如何将Ajax变量值传递给Codeigniter中的控制器 - How to pass ajax variable value to controller in codeigniter 使用AJAX将javascript数组传递给CodeIgniter控制器 - Using AJAX to pass javascript array to CodeIgniter controller 如何将params传递给Rails控制器方法然后使用JQuery / AJAX返回值? - How do I pass params to Rails controller method then return value using JQuery/AJAX? 我想使用 ajax 在 post 方法中传递多个参数。 - I want to pass multiple parameters in post method using ajax. 将值从Controller传递到jQuery CodeIgniter - Pass a value from Controller to jQuery CodeIgniter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM