I hate to break it to you, but this is totally incorrect:
Quote:
quote:Originally posted by sebas
Actually, the problem is if the boxes are checked on the same page there'll be no problems (as you have shown). But, if the on/off status of the checkboxes have to be passed to a secondary page, then the error message will appear (especially if the boxes are not checked). This way the variables have to be controlled/checked.
If everything are to be kept on one page, then not many problems will occur. This matter Joanncae have not yet encountered, because she is still in the third chapter (I know, because I have looked into the 'Beginning PHP') and the application of your 'foreach...' is in the fifth chapter. ;)
|
It doesn't matter if all the logic is on the same page or not. When you submit a form, you're making a completely new request from the web server. The PHP script runs from the top each time, totally unaware of how many times it may have been run for any number of users.
HTTP is a stateless protocol. The closest you can come to maintaining state across page requests is to store variables in cookies or sessions, but those are more advanced topics that we won't discuss today.
Here's another version of the test script just to prove my statements:
<html>
<head><title>Checkbox form</title></head>
<body>
<form method="post" action="form.php">
<input type="checkbox" name="authors[]" value="J.R.R. Tolkien">J.R.R. Tolkien</input><br />
<input type="checkbox" name="authors[]" value="Dan Brown">Dan Brown</input><br />
<input type="checkbox" name="authors[]" value="Gabriel Garcia Marquez">Gabriel Garcia Marquez</input><br />
<input type="checkbox" name="authors[]" value="F. Scott Fitzgerald">F. Scott Fitzgerald</input><br />
<input type="checkbox" name="authors[]" value="Nikolai Devereaux">Nikolai Devereaux</input><br />
<input type="submit" name="submit" value="Submit" />
</body>
</html>
<?php // form.php -- totally separate file from the .html above
if (isset($_POST['authors']))
{
echo "You chose: " . join(', ', $_POST['authors']) . ".\n";
}
echo "<br />\n";
echo "<a href=\"form.html\">Choose again</a>\n";
?>
The reason you get an undefined index error is not necessarily due to register_globals, it has to do with a new default error_reporting setting. Older versions of PHP suppressed E_NOTICE errors because it was considered a "feature" of PHP that uninitialized variables would be created on-the-fly when needed. The PHP guys finally decided that this was, in fact, a Bad Thing and changed the setting.
For more info on this setting and the reasons behind the change, read:
http://www.php.net/security.errors
Take care,
Nik
http://www.bigaction.org/