tags:

views:

33

answers:

2

I was wondering how can I validate a single checkbox using PHP and MySQL.

Here is the HTML.

<input type="checkbox" name="privacy_policy" id="privacy_policy" value="yes" />
+2  A: 

If isset($_REQUEST['privacy_policy']) returns true, they ticked the box. If not, they didn't.

Tim Green
You can always replace $_REQUEST with $_GET or $_POST but you didn't specify which method you're using so I settled for $_REQUEST.
Tim Green
For extra validation, you can always make sure the submitted value equals whatever you'd send out in the first place, e.g. `$_REQUEST['privacy_policy'] == 'yes'`
Marc B
Fair point, but note to anyone who implements this, you should use the above in a conjunction with the isset on the left, as if the object isn't set, then PHP will throw an E_NOTICE which will be displayed with an E_ALL error_reporting level which is bad practice.
Tim Green
A: 

Although you have asked for PHP validation, you should probably provide javascript validation on the client site as well as PHP validation. This provides a more rapid response than server side validation.

It's important to have both because

  • users may not have javascript turned on
  • you can't trust anything from the client side anyway :)

If you're using jQuery for anything else, the jQuery Validation plugin makes client side validation very straightforward.

El Yobo