Sending Data Along with a Redirect in Codeigniter

How to pass a data with redirect in codeigniter

So in the controller you can have in one function :

$in=1;
redirect(base_url()."home/index/".$in);

And in the target function you can access the $in value like this :

$in = $this->uri->segment(3);   
if(!is_numeric($in))
{
redirect();
}else{
if($in == 1){

}
}

I put segment(3) because on your example $in is after 2 dashes. But if you have for example this link structure : www.mydomain.com/subdomain/home/index/$in you'll have to use segment(4).

Hope that helps.

Sending data along with a redirect in CodeIgniter

I believe redirect uses header(). If so, I don't believe you can send data along with a location header. You could accomplish the same thing using session vars or (not as good) appending a query string to the location URL.

For an 'accepted' way to do this in CodeIgniter look a little more than halfway down the session class documentation page.

CodeIgniter supports "flashdata", or session data that will only be available for the next server request, and are then automatically cleared. These can be very useful, and are typically used for informational or status messages (for example: "record 2 deleted").

This (now deleted - here's an archived version) post on flash messages covers both the query string and the session var method.

Update: To summarize the now deleted post, it showed both urlencoding a message and appending as a query string (example from post):

header('Location: http://www.example.com/index.php?message='.urlencode($message));

And setting a 'flash' variable using two frameworks (example from post):

//Zend Framework
$flashMessenger = $this->_helper->FlashMessenger;
$flashMessenger->setNamespace('actionErrors');
$flashMessenger->addMessage($message);

//CakePHP
$this->Session->setFlash('Your post has been saved.');
$this->redirect('/news/index');

Of course you can do roughly the same thing using $_SESSION directly (my example):

//first request
$_SESSION['flash'] = 'This is a simple flash message.';
//next request
$flash = $_SESSION['flash'];
unset($_SESSION['flash']); //flash is one time only

Can I pass data to view if I use redirect in codeigniter framework?

There are two possible ways, you can pass values while redirecting the user.

1. Using Sessions:
Use session to pass data while redirecting. There is a special method in CodeIgniter to do it called set_flashdata

$this->session->set_flashdata('in',1);
redirect("home/index");

Now you may get in your index controller like

function index()
{
$in = $this->session->flashdata('in');
if($in==1)
{

}
}

Remember this data will available only for redirect and lost on next
page request. If you need stable data then you can use URL with
parameter & GET $this->input->get('param1')

2. Using Get values

So in the controller you can have in one function :

$value=1;
redirect(base_url()."home/index/".$value);

And in the target function you can access the $value value like this :

$value= $this->uri->segment(3);   
if(!is_numeric($value))
{
redirect();
}else{
if($value == 1){

}
}

I put segment(3) because in your example $value is after 2 dashes. But if you have for example this link structure: www.mydomain.com/subdomain/home/index/$value you'll have to use segment(4).

Hope it helps!

post data via redirect function in codeigniter

when using redirect you go from one controller or another by this process all post data are destroyed unless you stored them on a session, here is how i do it

$data = array('firstname'=>'fname','lastname'=>'lastname');
// i store data to flashdata
$this->session->set_flashdata('lolwut',$data);
// after storing i redirect it to the controller
redirect('controller/method')

so on your redirected controller you can access it via $this->session->flashdata('lolwut')
note that i am using flashdata not userdata, flashdata destroys itself on the next process.

read more flashdata here SESSION CLASS

How to Pass data by using redirect in codeigniter

You can use codeigniter session's set_flashdata() method

{

$body = $e->getJsonBody();
$err = $body['error'];
$data['failure_response'] = $err;
$data['response_status'] = $e->getHttpStatus();
$where = array('id' => $this->session->userdata('id'));
$payment_info = $this->baseM->getOneRowData('users', $where);

/* EITHER */
$this->session->set_flashdata('failure_response' ,$err);
$this->session->set_flashdata('failure_response' ,$e->getHttpStatus());
/* OR */
$this->session->set_flashdata($data);

redirect('service/failure_402');
}

In falure_402()

public function failure_402()
{
print_r($this->session->flashdata());
/*OR this way */
echo $this->session->flashdata('failure_response')
}

For more : https://www.codeigniter.com/user_guide/libraries/sessions.html#flashdata

How to send variable data when redirecting to previous page

You can use set_flashdata of CI.You can use only once that message after refresh page message goes blank.

  $this->session->set_flashdata('message', 'Authentication failed');

redirect(site_url('message/index/'), 'refresh');

And on that page you can catch this message by

$message = $this->session->flashdata('message').

How to pass a POST value to a function after a redirect in a CodeIgniter controller

try this code, you have use session for this

function myfunction1(){
$data = array(
'code' => $this->mymodel->myautocode(),
'value1' => $this->input->post('value1')
);
$this->session->set_userdata($data);
redirect(mycontrollers/myfunction2);
}

function myfunction2(){
$value2 = $this->session->userdata('value1');
$code1 = $this->session->userdata('code');
}


Related Topics



Leave a reply



Submit