Jump to content

Archived

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

w3sl3y2003

Form handling

Recommended Posts

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>
<?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>[/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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
no, just use explicit declarations/definitions such as:
[code]<?php

$products = $_POST['products'];

?>[/code]

or just go straight for:
[code]<?php

foreach ($_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.

Share this post


Link to post
Share on other sites

×

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.