简体   繁体   English

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

[英]Sending data using AJAX and redirect in PHP

I am sending data when a button is clicked using AJAX. 使用AJAX单击按钮时,我正在发送数据。 When data is sent to PHP server(Codeigniter framework), it will do some processing on server and retrieve the data from the database and should then redirect to another page with the retrieved data, 当数据发送到PHP服务器(Codeigniter框架)时,它将在服务器上进行一些处理并从数据库中检索数据,然后应将检索到的数据重定向到另一个页面,

Below is the AJAX call, 下面是AJAX调用,

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

Below is the PHP processing, 以下是PHP处理,

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

If I use the above PHP code, then it will not redirect to another page, but it will try to send the complete view file as AJAX an response(which I am able to see in the browser via inspect), 如果我使用上面的PHP代码,那么它将不会重定向到另一个页面,但是它将尝试将完整的视图文件作为AJAX发送一个响应(我可以通过inspect在浏览器中看到),

So my question is, how can I perform a redirect with my complete $data array? 所以我的问题是,如何使用完整的$data数组执行重定向?

First you have to redirect to your desired view with id in parameter like 首先,您必须使用参数中的id重定向到所需的视图,例如

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

after redirecting to URL then you should have send the ajax call 重定向到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);
}
});

Now you have your complete data on the redirected page. 现在,您在重定向页面上拥有了完整的数据。

You can append it to any div with jQuery. 您可以使用jQuery将其附加到任何div。

Try like this...In php use have to use json_encode() for sending back response as below.. 尝试这样...在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);

Then in your AJAX.. 然后在您的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
}
});

You are using ajax in a wrong way! 您以错误的方式使用ajax!

When a php proccess is called via ajax, redirecting in php is not meaningful any more. 当通过ajax调用php过程时,在php中重定向不再有意义。 After you retrieve data via ajax you should do redirect in jQuery. 通过ajax检索数据后,应该在jQuery中进行重定向。

Firstly just echo json data in php : 首先,仅在php中回显json数据:

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

echo json_encode($data);

then redirect when data retrieved : 然后在检索数据时重定向:

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

now in /user/report view pars json with jQuery for usage 现在在/user/report视图中,使用jQuery解析json以供使用

UPDATE: 更新:

I think you dont need ajax at all! 我认为您根本不需要ajax! Ajax is to help us not having redirection! Ajax旨在帮助我们避免重定向!

Try this : 尝试这个 :

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

and in your php : 并在您的php中:

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

If using code igniter you could try using flashdata (temporary session) and header 如果使用代码点火器,则可以尝试使用flashdata (临时会话)和header

Try: 尝试:

$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;

Then in your user/report to get the data use : 然后在您的user/report中获取数据使用:

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

BUT I think you don't need ajax at all, in the first place, if you use standard html and POST the data to your admin/reports/ using the plain web form with action="admin/reports/" . 我认为你不需要阿贾克斯可言,摆在首位,如果使用标准htmlPOST数据到您的admin/reports/使用简单的web formaction="admin/reports/" In this case your PHP can exactly be the same as in OP 在这种情况下,您的PHP可以与OP中的完全相同

I removed AJAX and did simple redirect in JS, 我删除了AJAX,并在JS中进行了简单的重定向,

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

Then PHP code, use rep_id as function variable and did processing and loaded View page, 然后是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