problem with HTML checkbox variable in PHP, please help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
orangeapple
Forum Commoner
Posts: 70
Joined: Tue Jan 06, 2004 1:24 pm
Location: Geneva / Switzerland

problem with HTML checkbox variable in PHP, please help

Post 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 ?
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post by microthick »

Maybe

if (isset($HTTP_POST_VARS["changecheck"])) {
$changecheck = $HTTP_POST_VARS["changecheck"]));
}
User avatar
orangeapple
Forum Commoner
Posts: 70
Joined: Tue Jan 06, 2004 1:24 pm
Location: Geneva / Switzerland

Post by orangeapple »

Great, thanks !

I'll try this as soon i'm home.
Can you explain your code ?
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post 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
User avatar
orangeapple
Forum Commoner
Posts: 70
Joined: Tue Jan 06, 2004 1:24 pm
Location: Geneva / Switzerland

Post 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.
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post 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.
Post Reply