Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

passing values between multiple pages

Status
Not open for further replies.

samyII

Programmer
Mar 22, 2005
14
IN
hi,
i have to pass values between mutiples pages, say text field value from page 1 to page 3. how can i achieve this. please help.
 
You could set up the link that you're clicking on page 2 to get to page 3 to pass the variable in on the URL. For example :

echo "<a href='page3.php?yourvar="$yourvariable"'>";

'yourvar' is the variable name that you can access your variable with on page 3.

If you have another form on page 2 you could use a hidden form field to pass the variable into page 3:

echo "<input type='hidden' name='yourvar' value='"$yourvariable"' />

I'm not an experienced user, so there may be better ways, but both of those will work.
 
If you want to keep track of variable values across a number of pages, your best bet is to do it with cookies. You can set a cookie on the client side, then read the cookie as you wish and use the data retrieved on any page on the website.

Lee
 
I Told you there could be a better way :)

My last bit of code should have read:

echo "<input type='hidden' name='yourvar' value='"$yourvariable"'>";

I forgot to finish it off.

Also, If you do try it either of the ways I suggested. I forgot to mention that if this is data from a text field then you'll have to make sure that there's nothing entered that will mess up your echo'd xhtml code. You could use htmlentities($yourvariable).
 
Using an input, each page will have to have the form submitted to the next page where the value is required. If the viewer goes to the page any other way, using the <input> won't work.

Lee
 
Your best bet is to use sessions. At the start of each script, before anything is sent to the screen, the following statement should appear:
Code:
session_start();
Then any value you wish to be seen by other scripts, should be stored in the $_SESSION array:
Code:
$_SESSION['somevar'] = $someval;
In one of your other scripts (or in the same script), you can reference the value with:
Code:
$someval = $_SESSION['somevar'];
Please see for more information.

Ken
 
i m using cookies to pass values between multiple forms. will also give a try to sessions.the input tag had the problem that it could be used in the referenced page not in all pages.so that idea was dropped
 
Session variables are just one kind of cookie, a temporary one.

Lee
 
To add to that, session variables have to be added server side, where cookies are added either on the server or on the client. If you're grabbing information from a page without it submitting, you'll have to use client-side cookies, though you can still access the values (I believe, have done this in ASP, but not tried in PHP yet) through server-side scripting.

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top