Page 1 of 1
beginner's trouble in checkbox
Posted: Mon Aug 11, 2003 9:28 pm
by lancet2003
Here is the code in one html file I run first
<HTML>
<HEAD></HEAD>
<BODY>
<FORM method="get" action="text.php">
WHo is your favourite authors?
<input type=submit>
<br>
<input name="choice1" type="checkbox" value="111" checked >
<input name="choice2" type="checkbox" value="222" checked >
<input name="choice3" type="checkbox" value="333" checked >
</FORM>
</BODY>
</HTML>
then here is the code of text.php
<HTML>
<HEAD></HEAD>
<BODY>
Your favorite author is:
<?php
echo $aaa;
echo $choice1;
echo $choice2;
echo $choice3;
?>
</BODY>
</HTML>
when I just check some of the checkboxes and then press submit.
The wrong message show up as following:
Notice: Undefined variable: choice3 in c:\inetpub\wwwroot\text.php on line 9
but in the book, it only mentioned that the the checkbox not ticked will not be transfer to PHP engint and will not show its value. so is there any configuration in PHP to delete this Notice information?
Thanks!
Posted: Mon Aug 11, 2003 9:39 pm
by JAM
Code: Select all
<?php
// Turn off all error reporting
error_reporting(0);
// Report simple running errors
error_reporting (E_ERROR | E_WARNING | E_PARSE);
// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings ...)
error_reporting (E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
// Report all errors except E_NOTICE
// This is the default value set in php.ini
error_reporting (E_ALL ^ E_NOTICE);
// Report all PHP errors (bitwise 63 may be used in PHP 3)
error_reporting (E_ALL);
// Same as error_reporting(E_ALL);
ini_set ('error_reporting', E_ALL);
?>
http://se.php.net/manual/en/function.er ... orting.php
But it's not recommended. Errors are good as they nag you everytime there is something badly done/missing and you get to learn something trying to fix it.
Your particular problem lies probably in that a checkbox is true/false depending on nature. If not ticked, it shouldn't send itself, and then you cant echo it.
Place following in your index.php:
Code: Select all
<pre>
<FORM method="post">
WHo is your favourite authors?
<input type=submit>
<br>
<input name="choice1" type="checkbox" value="1" checked >1
<input name="choice2" type="checkbox" value="2" >2
<input name="choice3" type="checkbox" value="3" checked >3
</FORM>
<b>BAD</b>:
<?php
echo $_POST['choice1']."\n";
echo $_POST['choice2']."\n";
echo $_POST['choice3']."\n";
?>
<b>BETTER</b>:
<?php
if (isset($_POST['choice1'])) { echo $_POST['choice1']."\n"; }
if (isset($_POST['choice2'])) { echo $_POST['choice2']."\n"; }
if (isset($_POST['choice3'])) { echo $_POST['choice3']."\n"; }
?>
And try running it. We could explain more... =)
thanks
Posted: Mon Aug 11, 2003 9:51 pm
by lancet2003
But it's not recommended. Errors are good as they nag you everytime there is something badly done/missing and you get to learn something trying to fix it.
BUt when I put my code in the server, when someone run it and choose none of checkbox, if this kind of information show up, will users feel uncomfortable.
I try this one
error_reporting(0);
then it is OK
About your last code, I can not understand, I just learned it 3 days ago. I am still a dummy now.
Thank you so much for your kind help, you are so cool:)
Posted: Mon Aug 11, 2003 9:59 pm
by Bennettman
JAM wrote:
Code: Select all
<b>BETTER</b>:
<?php
if (isset($_POST['choice1'])) { echo $_POST['choice1']."\n"; }
if (isset($_POST['choice2'])) { echo $_POST['choice2']."\n"; }
if (isset($_POST['choice3'])) { echo $_POST['choice3']."\n"; }
?>
Basically:
if (isset(variable)) means "if the variable is set (has a value)".
$_POST['variable'] is the same as
$variable (if register_globals is on), it just means it's using variables sent from a POST form. So
$_POST['choice1'] is the same as
$choice1
{ statements } runs all the statements inside the brackets, if the
if isset statement is true.
\n is a new line in PHP.
So, the whole thing (the $choice1 line) means that "if $choice1 is set, echo/print $choice1 then a new line" - if the $choice1 checkbox wasn't checked, it won't print $choice1.
Posted: Mon Aug 11, 2003 10:06 pm
by JAM
Thanks Bennetman... and yes, perhaps I should clarify the code after typing it...
I am still a dummy now.
Yah well, we all know that feeling ourselves... ;)
Code: Select all
BUt when I put my code in the server, when someone run it and choose none of checkbox, if this kind of information show up, will users feel uncomfortable.
Of course! Thats why you do not disable the errors, learn from them, and make it working (with help, without help).
Shortcuts will come around and bite you in the arse later on, trust me. And as you are so new to it, it much easier to start using it right now, than in 2 months.
Thanks all!
Posted: Mon Aug 11, 2003 10:35 pm
by lancet2003