How to Pass Multiple Variables Across Multiple Pages

Passing multiple variables between pages and using them

You can put as many values as you like on the query string. (Though as query strings get very long the web server would eventually impose a limit.) Here you simply append one key/value pair:

Response.Redirect("Results.aspx?cblvalues=" + cblvalues);

Just use a & to separate additional key/value pairs:

Response.Redirect("Results.aspx?cblvalues=" + cblvalues + "&moreValue=" + moreValues);

If you do get to the point where the query string becomes absurdly long and you basically have a lot of data to pass to the next page, then you'd be looking at other ways of doing this. A simple alternative may be to store the values in session state, redirect the user, then pull the values from session state. Something as simple as this:

Session["cblvalues"] = cblvalues;
Session["moreValues"] = moreValues;
Response.Redirect("Results.aspx");

Then in Results.aspx you can get the values:

var cblValues = Session["cblvalues"];
// etc.

You might also clear the session values once you get them, if the session doesn't need to keep carrying them:

Session.Remove("cblvalues");

How to use $_SESSION to pass on values from multiple pages and $_POST together

just add $_POST to $_SESSION

<?php

$_SESSION['firstname'] = $_POST['firstname'];
$_SESSION['lastname'] = $_POST['lastname'];

?>

but in "post.php" you can't call firstname and lastname with post,

$message = $_SESSION['firstname'];
$message .= $_SESSION['lastname'];
$message .= $_POST['door'];
$message .= $_POST['zip'];

don't forget to use session_start at the beginning script

or you can add again to tag input without $_SESSION inside form

<input name="firstname" type="hidden" value="<?php echo $_POST['firstname']; ?>" />
<input name="lastname" type="hidden" value="<?php echo $_POST['lastname']; ?>" />

Passing multiple variables to another page in url

Use the ampersand & to glue variables together:

$url = "http://localhost/main.php?email=$email_address&event_id=$event_id";
// ^ start of vars ^next var

How do I share a PHP variable between multiple pages?

Use $_SESSION -!

session_start();
$_SESSION['username'] = $_POST['username'];

You of course want to filter/sanitize/validate your $_POST data, but that is outside of the scope of this question...

As long as you call session_start(); before you use $_SESSION - the values in the $_SESSION array will persist across pages until the user closes the browser.

If you want to end the session before that, like in a logout button --- use session_destroy()

Using PHP variables on multiple pages

This is what the GET and POST 'super' globals are for.
http://www.tizag.com/phpT/postget.php



Related Topics



Leave a reply



Submit