icon Get the most out of Surmunity, read our tips here! Need an interesting blog to read? You've got to read the Surpass Blog! | Welcome! Please register to access all of our features.

» Surpass Web Hosting Forums » Discussions » PHP, MySQL » How to make sure a checkbox is checked?

PHP, MySQL General PHP questions. Or go to our PHPsuexec Forum >>

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread
Old August 23rd, 2004, 3:56 AM   #1 (permalink)
I own you!
Excelling Contributor
 
Joined in Apr 2004
563 posts
Gave thanks: 0
Thanked 3 times
How to make sure a checkbox is checked?

Hi,

Well at the bottom of the form, I have the submit button. Just above it, I have a checkbox that is required to be checked in order to submit the form (It is just asking the user that they have read and agreed to the terms of use.) So I need it to be checked by the user.

So anyone know how to do this?

Thanks
AJPayne is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old August 23rd, 2004, 4:05 AM   #2 (permalink)
H
after g, before i
Resident.
 
H's Avatar
 
Joined in Jul 2004
Lives in N,BC,CA
8,092 posts
Gave thanks: 48
Thanked 131 times
Well, you give it a value right? If it's checked it has a value, otherwise it doesn't. You could do something like:
Code:
if ($checkbox==null) {
echo "Sorry checkbox blah blah blah";
} else{
echo "Hurray! You checked the box";
}
At least that's the way I remember it.. Think it's time for bed.
H is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old August 24th, 2004, 4:34 PM   #3 (permalink)
Third Plateau
Comfy Contributor
 
Dave's Avatar
 
Joined in Apr 2004
Lives in East Hanover, New Jersey
Hosted on Nifty
272 posts
Gave thanks: 0
Thanked 0 times
I remember that $_POST['checkbox'] was set to either "on" or "off", but I don't really feel like validating that now.
__________________
site (syberdave.net) - server (nifty)
Dave is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old August 24th, 2004, 7:14 PM   #4 (permalink)
Surpass Fan
Seasoned Poster
 
Joined in Aug 2004
Lives in North Carolina, USA
Hosted on Pass11
46 posts
Gave thanks: 0
Thanked 0 times
Your HTML checkbox code needs to look something like this:
Code:
<input type="checkbox" name="myCheckbox" value="123">
Haugland is right I think.
A checked checkbox will have the value specified by the value attribute.
An unchecked checkbox will be NULL (pretty sure, maybe not set... it will evaluate to FALSE either way...).

There's several ways you could write the conditional and make it work. Here's one way.
PHP Code:
if ( $_POST['myCheckbox'] == true )
  
//checked
else
  
//unchecked 
__________________
All your base are belong to us.
AngryPeasant is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old August 24th, 2004, 8:36 PM   #5 (permalink)
Pixel Smasher
Comfy Contributor
 
MrPixar's Avatar
 
Joined in Feb 2004
Lives in Miami, FL
Hosted on Pass51
139 posts
Gave thanks: 0
Thanked 1 Time in 1 Post
let's work on the example of:
Code:
<input type="checkbox" name="myCheckbox" value="123">
when you submit the form you will be able to access the value by simply using

Code:
$myCheckBox
Note: if the checkbox is UNchecked the variable will not exist. Haugland's code sample is correct

EDIT: I forgot to add that there is nothing wrong with using the

Code:
$_POST['myCheckbox']
method for retreiving variables from the query string, if you prefer but generally any variable data that gets posted from a form submit is directly available to PHP.

Hope that helps
__________________
Pass51 | garyzullo.com
MrPixar is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old August 25th, 2004, 5:26 AM   #6 (permalink)
Registered User
Seasoned Poster
 
Joined in Jul 2004
31 posts
Gave thanks: 0
Thanked 0 times
Quote:
Originally Posted by MrPixar
EDIT: I forgot to add that there is nothing wrong with using the

Code:
$_POST['myCheckbox']
method for retreiving variables from the query string, if you prefer but generally any variable data that gets posted from a form submit is directly available to PHP.

Hope that helps
Not only is there nothing wrong with use the $_POST, $_GET, $_COOKIE and $_REQUEST superglobal arrays to retrieve variable data from a form, URI or cookie but it is hugely preferrable if you intend on writing portable PHP scripts. The ability to "just use" variables specified by the POST or GET method relies on a PHP setting called register_globals which was changed to "off" by default in PHP 4.2 and above.

This means that any script NOT using the superglobal arrays may mysteriously fail to work on some servers.

Register globals was switched off by default because it represented a possible security risk due to mismanagement of variables. Details on the decision can be read here:

http://www.php.net/register_globals

-wolf
Wolfy is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old August 25th, 2004, 5:47 AM   #7 (permalink)
the one who was
Super #1
 
patrickb's Avatar
 
Joined in Jul 2003
Lives in Memphis
1,967 posts
Gave thanks: 0
Thanked 3 times
Wolfy is on the money here. Except, register_globals is on by default on all of our servers. Too many scripts break without it. I highly do not recommend the use of just $myCheckBox by itself. You should always explicitly specify where the variable is coming from (IE: $_POST['myCheckBox']). Many scripts have been hacked and exploited because they always assumed that the variables would be from the right place. Its a very poor coding practice that needs to be avoided (sorry MrPixar).

As far as the value, the variable itself will not even exist in the $_POST array if it wasn't check. So, a simple isset($_POST['myCheckBox']) call is all that is needed to verify if it was checked.
__________________
Patrick

Warnings: The program(s) might crash unexpectedly or behave otherwise strangely. (But of course, so do many commercial programs on Windows.) --www.gimp.org
patrickb is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old August 25th, 2004, 5:48 AM   #8 (permalink)
Registered User
Seasoned Poster
 
Joined in Jul 2004
31 posts
Gave thanks: 0
Thanked 0 times
There seems to be massive confusion over what is sent to the PHP script in the POST variable to represent a checkbox. To decode whats happening you need to know two things:

1) PHP does not know (or care) whether your form used a checkbox, radiobox, a textarea or your pet dog "Claudine". It ONLY knows what was passed in the URI.

2) The HTML code:

Code:
<input name="myCheckbox" value="YoMamma" type="checkbox"/>
Results in a URI of the form http://mydomain.com/myscript.php?myCheckbox=YoMamma if it is checked or http://mydomain.com/myscript.php if it is not checked.

THEREFORE, to discover whether the checkbox in the original form was checked we need to check for the existence of myCheckbox in the superglobal array as so:

PHP Code:
if (isset($_POST['myCheckbox'])) {
  
// the checkbox was ticked; show sign up page
}
else
{
  
// they didn't agree; prompt them

Something else occurs to me: as well as the PHP check something as simple as this could be checked using JavaScript. I recommend checking for these things both on the client side (using JavaScript) and on the server side (PHP). Not only does this provide a double check but it means that the user can be made aware of the problem without having to wait for the form to be submitted.

Sample code for checking whether a checkbox has been completed in JS below:

Code:
...
<head>
<script type="text/javascript" language="Javascript">
<!--
function checkForm() {
  if (document.getElementByID("agree").checked) {
    return true; // submit the form
  }
  else
  {
      alert("You must agree to our Terms of Service and Acceptable Use Policy.");
      return false;  // do not submit the form
  }
}
//-->
</script>
</head>
<body>
<form action="signup.php" method="post" onsubmit="checkForm();">
...
  <input name="agree" id="agree" type="checkbox" value="yes"/>
...
</form>
</body>
...
Hope this helps.

-wolf

Last edited by Wolfy; August 25th, 2004 at 5:55 AM.. Reason: Added comment tags around JS to stop it showing in non-supporting browsers.
Wolfy is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old August 25th, 2004, 5:51 AM   #9 (permalink)
Registered User
Seasoned Poster
 
Joined in Jul 2004
31 posts
Gave thanks: 0
Thanked 0 times
Ick. Patrickb beat me to the right answer before I could post!

Oh well; at least the JS might prove useful to someone else.

-wolf
Wolfy is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On