I want to send one variable's value to another page.. How can i do with php..??
actually I'm receiving it from another page so i want to send it to another page..
I want to send one variable's value to another page.. How can i do with php..??
actually I'm receiving it from another page so i want to send it to another page..
Good question. is right about your options, and since you do not want the value visible in the URL (GET), @Zagga’s POST example is on the right track. If you want to pass the value without an extra click, store it in a session and redirect. This keeps it out of the URL and does not rely on hidden fields.
Example flow using sessions:
// pageA.php (receive value and forward)
session_start();
$value = isset($_POST['order_id']) ? $_POST['order_id'] : null; // or wherever it came from
// Validate/sanitize $value before storing if it influences queries, file paths, etc.
$_SESSION['order_id'] = $value;
header('Location: pageB.php'); // no value in URL
exit; // pageB.php (use the value)
session_start();
$orderId = isset($_SESSION['order_id']) ? $_SESSION['order_id'] : null;
// Use it, then optionally clear it if it is one-time data (flash-style):
unset($_SESSION['order_id']); Notes:
session_start() at the very top of any script that sets or reads session data, before any output.session_regenerate_id(true);.If you must POST to the next page without showing the value and without a user click, you can auto-submit a small form (building on @Zagga’s idea). Remember that hidden inputs are user-modifiable, so treat them as untrusted:
// pageA.php
$value = '12345'; // validated/escaped for HTML attribute context
?>
<form id="relay" method="post" action="pageB.php">
<input type="hidden" name="order_id" value="<?php echo htmlspecialchars($value, ENT_QUOTES, 'UTF-8'); ?>">
</form>
<script>document.getElementById('relay').submit();</script>
<?php Use sessions for anything sensitive; use GET only for non-sensitive identifiers you do not mind being bookmarked or logged.
Jump to Post— blocblue 238Hello,
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 …
Jump to Post— Member #671080Hi kirtan_thakkar,
If you want to use the POST method, you will need to make a form on the first page that would look something like . . .
<form method="post" action="your_new_page.php"> <input type="hidden" name="your_variable_name" value="your_variable_value" /> <input type="submit" name="Submit" value="Submit" /></td> </form>and then on …
Hello,
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?
R.
Yes but i don't want to show it on url. How can i do it from the post method??
Hi kirtan_thakkar,
If you want to use the POST method, you will need to make a form on the first page that would look something like . . .
<form method="post" action="your_new_page.php">
<input type="hidden" name="your_variable_name" value="your_variable_value" />
<input type="submit" name="Submit" value="Submit" /></td>
</form> and then on the second page you can retrieve the variable with something like . . .
<?php
if (isset($_POST['your_variable_name'])){
$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.
Hope this helps.
Zagga
Thanks Buddy.. It is what I wanted...
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.