w3sl3y2003 Posted August 16, 2006 Share Posted August 16, 2006 Hi all,I'm just starting with form handling and i have a problem with passing variables between forms.I have 2 forms eg9.4.php [b]<html><head><title>Listing 9.4 An HTML form including a SELECT element</title></head><body><form action="eg9.5.php" method="POST"><input type="text" name="user"><br><textarea name="address" rows="5" cols="40"></textarea><br><select name="products[]" multiple><option>Sonic Screwdriver<option>Tricorder<option>ORAC AI<option>HAL 2000</select><br><input type="submit" value="hit it!"></form></body></html>[/b]and eg9.5.php[b]<html><head><title>Listing 9.5 Reading input from the form in Listing 9.4</title></head><body><?phpprint "Welcome <b>$user</b><p>\n\n";print "Your address is:<p>\n\n<b>$address</b><p>\n\n";print "Your product choices are:<p>\n\n";print "<ul>\n\n";foreach ( $products as $value ){print "<li>$value<br>\n";}print "</ul>";?></body></html>[/b]when i click on the button in eg9.4.php it's supposed to show me what i choose and display it on eg9.5.php. but when i do that nothing comes up on 9.5 except [b]Warning: Invalid argument supplied for foreach() in /srv/www/htdocs/app_ed/eg9.5.php on line 11[/b]. When i run through all of the [b]$HTTP_POST_VARS[/b] it shows me all of the posted variables including what i entered in the textbox and textarea which is what i wanted.Where could i be going wrong? Quote Link to comment Share on other sites More sharing options...
Jenk Posted August 16, 2006 Share Posted August 16, 2006 none of your variables are defined before you try to obtain their supposed values.e.g. $products doesn't exist until you try to foreach() it. Quote Link to comment Share on other sites More sharing options...
w3sl3y2003 Posted August 16, 2006 Author Share Posted August 16, 2006 php variables are dynamic heap variables so they don't need to be declared/defined prior to their use. Quote Link to comment Share on other sites More sharing options...
Jenk Posted August 16, 2006 Share Posted August 16, 2006 Yes they do.register_globals is now set to Off by default and will be removed completely from php6 onwards.It is also bad practice to rely upon register_globals.also, even if you are relying on register_globals, are they even defined in the $_GET, $_POST, $_COOKIE, $_SERVER or $_FILES superglobal arrays? Quote Link to comment Share on other sites More sharing options...
w3sl3y2003 Posted August 16, 2006 Author Share Posted August 16, 2006 Sorry for being rude - thanx for the replies guys!!!i've modified the code and used [b]$_POST[/b] to "initialise" the variables but i still don't get anything. should i set [b]register_globals[/b] to yes? Quote Link to comment Share on other sites More sharing options...
Jenk Posted August 16, 2006 Share Posted August 16, 2006 no, just use explicit declarations/definitions such as:[code]<?php$products = $_POST['products'];?>[/code]or just go straight for:[code]<?phpforeach ($_POST['products'] as $product) {//etc?>[/code]but remember to sanitise your input for its relevant purpose, and to check if there is any input in the first place. Quote Link to comment Share on other sites More sharing options...
w3sl3y2003 Posted August 16, 2006 Author Share Posted August 16, 2006 Found the problem! Guess who's the idiot who has a typo in his source >:(Thanks effort though guys!!!!!!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.