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 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

3 Comments

You can always replace $_REQUEST with $_GET or $_POST but you didn't specify which method you're using so I settled for $_REQUEST.
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'
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.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.