繁体   English   中英

使用AJAX发送数据并在PHP中重定向

[英]Sending data using AJAX and redirect in PHP

使用AJAX单击按钮时,我正在发送数据。 当数据发送到PHP服务器(Codeigniter框架)时,它将在服务器上进行一些处理并从数据库中检索数据,然后应将检索到的数据重定向到另一个页面,

下面是AJAX调用,

jQuery.ajax({
 url: baseURL + "admin/reports/",
 data: {rep_id:report_ids},
 type: "POST",
 success:function(data){
 var objData = jQuery.parseJSON(data);
}
});

以下是PHP处理,

$data = $this->admin_model->fetch_date($this->input->post('rep_id', TRUE));
$this->load->view('user/report', $data);

如果我使用上面的PHP代码,那么它将不会重定向到另一个页面,但是它将尝试将完整的视图文件作为AJAX发送一个响应(我可以通过inspect在浏览器中看到),

所以我的问题是,如何使用完整的$data数组执行重定向?

首先,您必须使用参数中的id重定向到所需的视图,例如

$para array( 'id'=>$report_ids);
redirect('admin/reports/'.$para);

重定向到URL之后,您应该已经发送了ajax调用

jQuery.ajax({
 url: baseURL + "admin/reports/",
 data: {rep_id:report_ids}, // Here you have to get your id from url with jQuery
 type: "POST",
 success:function(data){
 var objData = jQuery.parseJSON(data);
}
});

现在,您在重定向页面上拥有了完整的数据。

您可以使用jQuery将其附加到任何div。

尝试这样...在php中,必须使用json_encode()来发送回响应,如下所示。

$data = $this->admin_model->fetch_date($this->input->post('rep_id', TRUE));//Fetch your data in array format

echo json_encode($data);

然后在您的AJAX中。

jQuery.ajax({
 url: baseURL + "admin/reports/",
 data: {rep_id:report_ids}, // Here you have to get your id from url with jQuery
 type: "POST",
 success:function(data){
 var objData = jQuery.parseJSON(data);//objData is in object format...
 //try  console.log(objData);
 window.location.href('user/report?name='+objData.name);//name is attribute of your object
}
});

您以错误的方式使用ajax!

当通过ajax调用php过程时,在php中重定向不再有意义。 通过ajax检索数据后,应该在jQuery中进行重定向。

首先,仅在php中回显json数据:

$data = $this->admin_model->fetch_date($this->input->post('rep_id', TRUE));//Fetch your data in array format

echo json_encode($data);

然后在检索数据时重定向:

jQuery.ajax({
 url: baseURL + "admin/reports/",
 data: {rep_id:report_ids}, // Here you have to get your id from url with jQuery
 type: "POST",
 success:function(data){
 // do not pars json!
 window.location = '/user/report?data=' + objData; // sending json as a string in query string
}
});

现在在/user/report视图中,使用jQuery解析json以供使用

更新:

我认为您根本不需要ajax! Ajax旨在帮助我们避免重定向!

尝试这个 :

<a href="/admin/report"> GET REPORT! </a>

并在您的php中:

$data = $this->admin_model->fetch_date($this->input->post('rep_id', TRUE));
$this->load->view('user/report', $data);

如果使用代码点火器,则可以尝试使用flashdata (临时会话)和header

尝试:

$data = $this->admin_model->fetch_date($this->input->post('rep_id', TRUE));
$this->session->set_flashdata('tempValue', '$data');
header("Location: http://yousite/user/report");
exit;

然后在您的user/report中获取数据使用:

$this->session->flashdata('tempValue');

我认为你不需要阿贾克斯可言,摆在首位,如果使用标准htmlPOST数据到您的admin/reports/使用简单的web formaction="admin/reports/" 在这种情况下,您的PHP可以与OP中的完全相同

我删除了AJAX,并在JS中进行了简单的重定向,

window.location = baseURL + 'admin/reports/test/' + rep_id;

然后是PHP代码,使用rep_id作为函数变量并进行处理并加载了View页面,

public function test($in)
{
  $data['test'] = $this->admin_model->fetch_insight($in);
  $this->load->view('user/report', $data);
}

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM