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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

posted variables

Status
Not open for further replies.

manicleek

Technical User
Jun 16, 2004
143
0
0
GB
I have a search results page which takes results according to either a name or a town.

I have used

$name = $_POST["name"]; and
$town = $_POST["town"];

this works fine for the first page of results, but when I move to the second I get an "undefined index" error

I assume this is because when moving to the second page its waiting for these variables to be filled with search criteria again

is there any way I can stop this from happening?
 
POST variables come from posting forms. when you move to the second page using any other method than posting a form that contains fields named 'name' and 'town' you will get such an error.

you can try to register those variables as session variables (which then can be accessed throughout the page as long as the session is valid), or pass them as GET variables to the second page (as arguments to the php page), or using a form to pass them around (instead of a general redirect or link, use a form that posts to the desired location).

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
you need to start a session (session_start())
then use $_SESSION["name"] = 'blabla' to store and $tempVar = $_SESSION["name"]

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
There is one more way to pass these vars:
You insert hidden fields in the second page which are filled with the values of the posted vars. You can create those "on-the-fly" by looping through the $_POST array:
Code:
foreach($_POST as $key=>$value){
   echo '<input type="hidden" name="'.$key.'" value="'.$value.'">'."\n";
}

I really like sessions, but for completeness of answers the above technique should be mentioned.
 
I already mentioned that in my first post:
" or using a form to pass them around (instead of a general redirect or link, use a form that posts to the desired location). "

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
Granted. It won't hurt though to spell it out a bit more in detail. You deserve your star.
 
:D

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top