There are numerous ways you can pass a variable from one page to another:
- Pass it on the URL and use $_GET to retrieve the value;
- Post the value to the new page and use $_POST to retrieve the value;
- Store the value in the $_SESSION array (this will require starting a session at the top of both pages);
- Store the value in a database;
The easiest option from those above would probably be the first one - passing the value on the URL. An example of doing this would be to add the variable name and variable value to the end of the URL in the href attribute of an anchor tag:
<a href="/path/to/next/page?variableName=variableValue" title="Link to next page">Link to next page</a>
You could then retrieve this on the next page using:
// Check that the variable is set
$variable = (isset($_GET['variableName'])) ? $_GET['variableName'] : null;
Do however make sure that you validate the content of the variable before using it, as it is obviously very easy to tamper with on the URL.
Does this answer your question?
by blocblue: n/a
Yes but i don't want to show it on url. How can i do it from the post method??
and then on the second page you can retrieve the variable with something like . . .
$new_variable = $_POST['your_variable_name'];
This method will require the user to click the "Submit" button on the form in the first page. If you want to pass the variable without any user input, then SESSIONS are probably the easiest way to do it.
As robothy said, make sure you validate the variable before you use it as there are ways to tamper with it even though it is not in the URL.