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

时间:2017-01-06 06:00:10

标签: php jquery ajax codeigniter

我在使用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发送响应(我可以通过检查在浏览器中看到),

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

5 个答案:

答案 0 :(得分:0)

首先,您必须使用参数中的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。

答案 1 :(得分:0)

尝试这样...在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
}
});

答案 2 :(得分:0)

你正在以错误的方式使用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);

答案 3 :(得分:0)

如果使用代码点火器,您可以尝试使用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数据,我认为您根本不需要ajax admin/reports/ 1}}使用普通web formaction="admin/reports/"。在这种情况下,您的PHP可能与OP

中的完全相同

答案 4 :(得分:0)

我删除了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);
}
相关问题