Jump to content


Photo

Form handling


  • Please log in to reply
6 replies to this topic

#1 w3sl3y2003

w3sl3y2003
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 16 August 2006 - 12:18 PM

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

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


and eg9.5.php

<html>
<head>
<title>Listing 9.5 Reading input from the form in Listing 9.4</title>
</head>
<body>
<?php
print "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>


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 Warning: Invalid argument supplied for foreach() in /srv/www/htdocs/app_ed/eg9.5.php on line 11. When i run through all of the $HTTP_POST_VARS 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?


#2 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 16 August 2006 - 12:45 PM

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.

#3 w3sl3y2003

w3sl3y2003
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 16 August 2006 - 12:47 PM

php variables are dynamic heap variables so they don't need to be declared/defined prior to their use.

#4 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 16 August 2006 - 12:52 PM

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?

#5 w3sl3y2003

w3sl3y2003
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 16 August 2006 - 12:59 PM

Sorry for being rude - thanx for the replies guys!!!

i've modified the code and used $_POST to "initialise" the variables but i still don't get anything. should i set register_globals to yes?

#6 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 16 August 2006 - 01:13 PM

no, just use explicit declarations/definitions such as:
<?php

$products = $_POST['products'];

?>

or just go straight for:
<?php

foreach ($_POST['products'] as $product) {
//etc

?>

but remember to sanitise your input for its relevant purpose, and to check if there is any input in the first place.

#7 w3sl3y2003

w3sl3y2003
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 16 August 2006 - 01:23 PM

Found the problem! Guess who's the idiot who has a typo in his source  >:(

Thanks effort though guys!!!!!!!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users