[英]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.