Jump to content

passing vareabls in links?


knowram

Recommended Posts

I am trying to create a back button on a form. would like to be able to send the variable $_REQUSET to the page that I am going back to. However I don't know what is used in that array to divide variables. so when I try to print it on the page that I am going back to all i get is  "ARRAY". here is the code that I am using
the like is:
[code]
<a href="diagnoses.php?backinfo=<?=$_REQUEST?>">Back To Diagnoses</a>
[/code]
diagnoses.php being the previous page that I am trying to go back to.
and the cod for on the diagnoses.php page to see if the info has been submitted and I can get to it is:
[code]
echo'<pre>';
print_r ($backinfo);
echo '</pre>';
[/code]
I have been able to submit arrays through links like this before but they have always been arrays that I have made so I have known how to split them up on the following page.

dose this make any sense? If so have any ideas?
Link to comment
https://forums.phpfreaks.com/topic/13920-passing-vareabls-in-links/
Share on other sites

The $_REQUEST var is an array of all $_GET and $_POST and $_SESSION variables.
The good way to pass all this vars from the array is to seperate them and putting
them into the $_POST array

[code]
<?php
foreach($_REQUEST as $key => $val) {
  $_POST[$key] = $val;
}
?>

<a href="diagnoses.php">Back To Diagnoses</a>
[/code]

Or you could pass them by $_GET aswell
[code]
<?php
$var_get = "?";
foreach($_REQUEST as $key => $val) {
  if($var_get != "?") {
    $var_get .= "&";
  }
  $var_get .= "$key=$val";
}
?>

<a href='diagnoses.php<?php echo "$var_get"; ?>'>Back To Diagnoses</a>
[/code]

Then on the next (or previous) page you can simply catch them again by the var $_REQUEST.
You could [url=http://php.net/serialize]serialize[/url] the array, put it in the link and then [url=http://php.net/unserialize]unserialize[/url] at the other page.

Example:
[code]
<?php
$data = serialize($_REQUEST);
echo "<a href='what-ever-page.php?data={$data}'>Back</a>";
?>
[/code]
(If you'd like to use POST or GET instead of REQUEST you just replace it)

Another solution would be to use [url=http://php.net/http_build_query]http_build_query[/url] to build a query string automaticly for you. You need to use PHP5 though.
Example:
[code]
<?php
$data = http_build_query($_REQUEST);
echo "<a href='what-ever-page.php?{$data}'>Back</a>";
?>
[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.