| PHP, MySQL General PHP questions. Or go to our PHPsuexec Forum >> |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread |
|
|
#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 |
|
|
|
|
|
#2 (permalink) |
|
after g, before i
Resident.
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";
}
|
|
|
|
|
|
#3 (permalink) |
|
Third Plateau
Comfy Contributor
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) |
|
|
|
|
|
#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"> 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:
__________________
All your base are belong to us. |
|
|
|
|
|
#5 (permalink) |
|
Pixel Smasher
Comfy Contributor
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"> Code:
$myCheckBox EDIT: I forgot to add that there is nothing wrong with using the Code:
$_POST['myCheckbox'] Hope that helps
__________________
Pass51
|
|
|
|
|
|
#6 (permalink) | |
|
Registered User
Seasoned Poster
Joined in Jul 2004
31 posts
Gave thanks: 0
Thanked 0 times
|
Quote:
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 |
|
|
|
|
|
|
#7 (permalink) |
|
the one who was
Super #1
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 |
|
|
|
|
|
#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"/> 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:
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>
...
-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. |
|
|
|