Page 1 of 1

problem with HTML checkbox variable in PHP, please help

Posted: Tue Jan 13, 2004 12:23 am
by orangeapple
Hi,

I'm having troubles with the following variable from an HTML form :

<input type="checkbox" value="1" name="changecheck">

there is only 1 checkbox in the HTML form, the value of "changeckeck" is either "1" if checked or nothing if not checked.

Here is the php code :

$changecheck=$HTTP_POST_VARS['changecheck'];
if ($changecheck !== "1")

This works fine if the checkbox is checked, but if it is not, there is an error message :

Notice: Undefined index: changecheck in c:\program files\easyphp1-7\www\microsent\pages\pass_answer.php on line 16
(Line 16 is : if ($changecheck !== "1")).

Who can help ?

Posted: Tue Jan 13, 2004 1:28 am
by microthick
Maybe

if (isset($HTTP_POST_VARS["changecheck"])) {
$changecheck = $HTTP_POST_VARS["changecheck"]));
}

Posted: Tue Jan 13, 2004 3:51 am
by orangeapple
Great, thanks !

I'll try this as soon i'm home.
Can you explain your code ?

Posted: Tue Jan 13, 2004 4:12 am
by JayBird

Code: Select all

if (isset($HTTP_POST_VARS["changecheck"])) { // If the checkbox has been check, then set the var
$changecheck = $HTTP_POST_VARS["changecheck"])); 
}
What was happening with your original code was that if the checkbox wasn't selected then $HTTP_POST_VARS['changecheck'] wouldn't exist, that's why you got the Undefined index error. So the code microthick gave you checks to see if the var exists, and if it does assign it to $changecheck.

Simple :)

Mark

Posted: Tue Jan 13, 2004 5:17 am
by patrikG
on a related note:

change $HTTP_POST_VARS to $_POST.

This is the new superglobal array wich will at some point replace $HTTP_POST_VARS entirely (and not run alongside it, as it the status currently). It will save you trouble later

Posted: Tue Jan 13, 2004 10:04 am
by orangeapple
Thanks Guys !

Actually, i had to add an "else" to make it work :

if (isset($HTTP_POST_VARS["changecheck"])) {
$changecheck = $HTTP_POST_VARS["changecheck"];
}
else
{ $changecheck = "0"; }


if ($changecheck !== "1")
{.....

Thanks for your comment patrickG, i'm going to change that.

Posted: Tue Jan 13, 2004 10:25 am
by scorphus
orangeapple wrote:

Code: Select all

if ($changecheck !== "1")
Just curious, are you sure you need to use the 'not identical' operator (!==) in place of the 'not equal' (!=)? Is there any reason why not just:

Code: Select all

if ($changecheck != "1")
Take a loot to the Comparison Operators section of the PHP Manual.

Regards,
Scorphus.