I am sending data when a button is clicked using 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,
Below is the AJAX call,
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,
$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),
So my question is, how can I perform a redirect with my complete $data
array?
First you have to redirect to your desired view with id in parameter like
$para array( 'id'=>$report_ids);
redirect('admin/reports/'.$para);
after redirecting to URL then you should have send the ajax call
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.
Try like this...In php use have to use json_encode()
for sending back response as below..
$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..
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!
When a php proccess is called via ajax, redirecting in php is not meaningful any more. After you retrieve data via ajax you should do redirect in jQuery.
Firstly just echo json data in php :
$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
UPDATE:
I think you dont need ajax at all! Ajax is to help us not having redirection!
Try this :
<a href="/admin/report"> GET REPORT! </a>
and in your 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
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 :
$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/"
. In this case your PHP
can exactly be the same as in OP
I removed AJAX and did simple redirect in 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,
public function test($in)
{
$data['test'] = $this->admin_model->fetch_insight($in);
$this->load->view('user/report', $data);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.