PHP Pass Data with Redirect

PHP Pass Data with Redirect

Set it as a $_SESSION value.

in page2:

$_SESSION['message'] = "Post successfully posted.";

in page1:

if(isset($_SESSION['message'])){
echo $_SESSION['message']; // display the message
unset($_SESSION['message']); // clear the value so that it doesn't display again
}

Make sure you have session_start() at the top of both scripts.

EDIT: Missed ) in if(isset($_SESSION['message']){

PHP. How to pass variable and auto redirect to another PHP file

Use the header function

<?php
...
$id = $user_profile['id'];
$_SESSION['id'] = $id;
header('Location: checkIfExsists.php?id='.$id);
?>

on the checkIfExsists.php get the variable with

$id = $_GET["id"];

That would solve your problem the way you want it to be solved, but, this isn´t neccesarilly the way it should be solved, maybe inside checkIfExists.php should be a class instead of structured code with a public function to check existance checkExistance, so you will then just need:

include_once(checkIfExists.php);
$check = new checker();
$exists = $check->checkExistance($id) ;

this way you do not have to be jumping between files and you can have a better way to re-use code,
regards.

PHP - Redirect and send data via POST

You can't do this using PHP.

As others have said, you could use cURL - but then the PHP code becomes the client rather than the browser.

If you must use POST, then the only way to do it would be to generate the populated form using PHP and use the window.onload hook to call javascript to submit the form.

Redirect and pass data from controller

If you want to pass data in a redirect, you can use the with() method.
You have to append it to the redirect like so:

redirect('/fortest2')->with('data', 'value');

It will be saved in your current session, so it will be only persistent until you refresh the page again. If you want to store it for longer you have to go with a database/textfile etc. You can then check for it using

if (session()->has('data')) { // check if it exists
$value = session('data'); // to retrieve value
}

You can also send errors with the redirect (from validation i.e.) using withErrors(), sending the current input with it using withInput()


For what you want to achieve, try using this in your controller. This will just send the users name with the redirect:

$user = DB::table('user2s')->where('name', $request->name)->first();
redirect('/fortest2')->with('username', $user->name);

You can then access is via session('username')

How to pass data when redirecting to another page?

You need to store your data in the session.

In order to pass data just once, there's a concept called flashing for you.

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.

PHP Redirect with POST data

Generate a form on Page B with all the required data and action set to Page C and submit it with JavaScript on page load. Your data will be sent to Page C without much hassle to the user.

This is the only way to do it. A redirect is a 303 HTTP header that you can read up on http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html, but I'll quote some of it:

The response to the request can be
found under a different URI and SHOULD
be retrieved using a GET method on
that resource. This method exists
primarily to allow the output of a
POST-activated script to redirect the
user agent to a selected resource. The
new URI is not a substitute reference
for the originally requested resource.
The 303 response MUST NOT be cached,
but the response to the second
(redirected) request might be
cacheable.

The only way to achieve what you're doing is with a intermediate page that sends the user to Page C. Here's a small/simple snippet on how you can achieve that:

<form id="myForm" action="Page_C.php" method="post">
<?php
foreach ($_POST as $a => $b) {
echo '<input type="hidden" name="'.htmlentities($a).'" value="'.htmlentities($b).'">';
}
?>
</form>
<script type="text/javascript">
document.getElementById('myForm').submit();
</script>

You should also have a simple "confirm" form inside a noscript tag to make sure users without Javascript will be able to use your service.

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!

Passing values to blade using redirect() and back() functions

You have 2 possibilities.

1 - Try something like :

return redirect()->back()->with('fee', $fee)
->with('vat', $vat)
->with('Transaction_vat', $Transaction_vat);

2 - Or like :

return redirect()->back()->with([
'fee' => $fee,
'vat' => $vat,
'Transaction_vat' => $Transaction_vat
]);


Related Topics



Leave a reply



Submit