Jump to content

carrying a _POST variable from page to page


jeff5656

Recommended Posts

I want to carry a variable from page to page and use it to display certain records in a table.

 

The form is simply

<form name="physician" method="post" action="displayreminders.php"> 
<table><tr><th>Physician (last name only)</th></tr>
<tr><td>
<input type="text" name="physician" id="physician" /></td></tr>
<tr><td><input type="submit" value="Submit"></td></tr></table></form>

 

The php action file (displayreminders.php) correctly displays all records WHERE $physician is same as the _POST. 

 

 

However, if I go to a new form and try to pre-populate it with $physician, it does not work.  On the new form I have:

 

if (isset($_POST['physician']))
    $physician = $_POST['physician'];
else
    $physician = '';

 

But when I echo $physician, it is blank!

How do I retain that original variable value all the way until a user closes the browser?

 

Thanks in advance...

You will have to set in a session variable. You cannot keep a $_POSt variable.

 

<?php
if(isset($_POST['physician'])) {
$_SESSION['physician'] = $_POST['physician'];
$physician = $_POST['physician'];
} elseif (isset($_SESSION['physician'])) {
$physician = $_SESSION['physician'];
} else {
$physician = '';
} ?> 

 

and make sure you put session_start(); at the top of every page you intend to use this session value.

and make sure you put session_start(); at the top of every page you intend to use this session value.

 

Ok thanks.  One other question: on subsequent pages that I use session_start, how do I refer to the variable?  Do I simply use

 

$physician OR

 

or do I say $physician = $_POST['physician'];

 

OR do I say $physician = $_SESSION['physician'];

 

The $_SESSION array will be populated automatically on session_start() with previously defined session variables... so you last response would be correct.

 

Next time, though, you can just test it for yourself. Try print_r($_SESSION) to output your currently defined session variables.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.