views:

459

answers:

5

Hi, i have been looking for this script for a while now. I have some rules and then i have a checkbox to click if you agree the terms and rules.

Now how do i make a check in PHP if the person has checked that box and agreed to the rules? Thanks for ur help, i appreciate it!

+1  A: 

form.php:

<form action="checkbox-form.php" method="post">
    <label for="formWheelchair">Do you need wheelchair access?</label>
    <input type="checkbox" name="formWheelchair" value="Yes" id="formWheelchair" />
 <input type="submit" name="formSubmit" value="Submit" />
</form>

checkbox-form.php:

if(isset($_POST['formWheelchair']) && 
$_POST['formWheelchair'] == 'Yes') 
{
    $wheelchair = true;
}
else
{
    $wheelchair = false;
}
    var_dump( $wheelchair );

// shorthand version:

    $wheelchair = isset($_POST['formWheelchair'])?true:false;

Straight from: http://www.html-form-guide.com/php-form/php-form-checkbox.html

Note: At a later point you may want to use sessions to store the data for server-side validation if the user has not entered in all the fields.

meder
@anon: why the downvote?
meder
-1 too much code. isset() is enough, a checkbox doesn't go into the $_POST array if it is not selected. $wheelchair = false; if (isset($_POST['formWheelchair'])) { $wheelchair = true; }
tharkun
one more thing: variable naming. 'formWheelchair' doesn't say enough. 'wheelchairNeeded' would be a better name.
tharkun
That's a silly excuse to downvote, but whatever. I personally prefer thorough, long answers than one liners.
meder
Well, 'formWheelchair' is a better example than 'myCheckbox', and at least I have the markup to go along with it.
meder
@meder: it's not an excuse, it's the reason for the downvote. You can disagree but IMO your answer doesn't promode good coding practices and good variable naming.
tharkun
yes, you're right with the variable naming, but I left it generic while you made a suggestion.
tharkun
I'm not promoting good coding practices because my $_POST checking is more thorough than yours? That makes a lot of sense...
meder
While you're at it, why don't you go downvote the answers which aren't exactly the same as yours?
meder
you're checking can lead ot errors in the long run, form values can change over time and by checking for a concrete value you introduce a potential source of error.
tharkun
oh hey meder, where's the problem! that's how the voting system works. I find some answers good and some answers I don't find helpful and that's how I vote. unlike many others I mostly explain my reasons for voting. you don't need to agree with it, but you also don't need to argue so much and get upset about it. that's just how SO works.
tharkun
The only reason I'm arguing is because that's a silly poor excuse for downvoting, yours is personal preference.
meder
There are some other answers that don't even rely on isset and instead the direct form field value, why aren't you downvoting those? At least be consistent... If I were to be anal I'd downvote yours but I'm not going to.
meder
'form values can change over time' - Well it would make sense if he updated the form that he would adjust the validation as well. If he was going to add/remove some form fields he will have to touch the validation regardless. Just because *you* think he'll only update the value of that checkbox and *you* assuming he won't update the form validation gives you reason to downvote?
meder
In addition, your answer sets one variable true on 1 condition, meaning it's half-answered because he'd have to either add an else clause or change the whole statement to a ternary, or use isset to see if that $userAgrees variable is checked. If you were to have provided a more definitive solution that would have been better.
meder
IMO your arguments only confirm my assessment that your design/coding strategies might not be optimal. If you can code something in a way that changes to it don't affect other parts of the software, you will code it like that. As for the conditional. It's enough to set the var to false beforehand. The code will execute faster that way.
tharkun
hm, and someone seems to agree with that.
tharkun
will removed my downvote, if you integrate the knowledge about the checkbox not being part of the $_POST array when not checked.
tharkun
@meder: Your check of the value is not required. As stated by @tharkun, a checkbox is only submitted if checked, so its presence in the $_GET or $_POST array is sufficient to establish that it was checked when the form was submitted, and checking the value is redundant. See the HTML spec, particularly section 17.13.2, "Successful controls": http://www.w3.org/TR/html4/interact/forms.html#h-17.13.2
NickFitz
May I ask why you need to involve the session for storing data during validation? Surely you simply re-print the form with most of the values intact, sans the invalid ones, and with appropriate error messages/indicators?
Rob
A: 

In form html:

<input type="checkbox" name="terms">

In php script that the form posts to:

if ( $_POST['terms'] == 'on' ) {

  echo 'User accepted terms';

}
code_burgar
Your code will throw undefined index 'terms' error. As when form get posted, php doesn't receive checkbox which was not checked. true code will be if(isset($_POST['terms'])) { /// } – Krishna Kant Sharma 0 secs ago
Krishna Kant Sharma
It will throw a notice, not an error.
code_burgar
Strictly speaking, this would work if the checkbox element was given a value attribute of "on", in that the box being checked would cause the condition to pass. However, you're right in saying that it would also throw a notice, which isn't the most graceful way of handling it. In a lot of cases, the value is immaterial, and you simply need to check for presence. The value does, however, become meaningful when setting up multiple options with the same name, where you'll get an array of selected options, or for testing radio buttons, where you need to determine which option was selected.
Rob
+1  A: 

If the form is a method POST form. Then on the action page you should have access to the $_POST variable.

Check out the results of this on your action page.

echo "<pre>";
print_r($_POST);
echo "</pre>";

The $_POST variable will be an array. You can access the value of the array like this.

if($_POST["key"] == "value")

Where the key is the name in the output above.

Ólafur Waage
+3  A: 

Assuming you have a form that looks something like this:

<form method="post" action="some_handler.php">
    <label for="option1">Option 1</label>
        <input id="option1" type="checkbox" name="option1" />
    <label for="option2">Option 2</label>
        <input id="option2" type="checkbox" name="option2" />
    <!-- submit, etc -->
</form>

You can check for the presence of the checkbox values (by name) in $_POST, i.e.

<?php
$optionOne = isset( $_POST['option1'] );
$optionTwo = isset( $_POST['option2'] );

If the boxes aren't checked, $_POST won't contain values for them.

Rob
+1 for reasons given in other comments :-)
NickFitz
+1  A: 

It's totally enough to check for:

$userAgrees = false;

if (isset($_POST['myCheckbox']))
{
   $userAgrees = true;
}
tharkun
+1 for understanding that the on/off state of checkboxes on the client is reflected in them being there/not there on the server :-)
NickFitz