Member Avatar for kirtan_thakkar

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..

Recommended Answers

All 5 Replies

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.

commented: Clear answer, well written and giving examples. I wish more answers (and questions) were like this :) +1
Member Avatar for kirtan_thakkar

Yes but i don't want to show it on url. How can i do it from the post method??

Member Avatar for Zagga

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

Member Avatar for kirtan_thakkar

Thanks Buddy.. It is what I wanted...

a

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.