Jump to content

What is the correct using php to see if a checkbox is checked or not.


jayhawker

Recommended Posts

if (isset($_POST['checkbox'])) {

 

If the value for the checkbox is checkbox then the above code would check to make sure it's set before going forward.

 

You mean to say if the "NAME" of the checkbox is "checkbox". . .

 

The isset() function doesn't care what the value is - as long as the variable has been initiated (it could even have a NULL value).

Link to comment
Share on other sites

if (isset($_POST['checkbox'])) {

 

If the value for the checkbox is checkbox then the above code would check to make sure it's set before going forward.

 

You mean to say if the "NAME" of the checkbox is "checkbox". . .

 

The isset() function doesn't care what the value is - as long as the variable has been initiated (it could even have a NULL value).

 

Yes I did mean to say that opps!  :P

Link to comment
Share on other sites

A checkbox field is only passed IF the checkbox is checked. So, the isset() status would be the appropriate method to determine if the checkbox was checked or not.

 

A checkbox field will be passed to PHP regardless of user input. The appropriate method would be to compare the value of the passed checkbox to the values you're expecting. Otherwise just check if it's empty or not with empty().

Link to comment
Share on other sites

A checkbox field will be passed to PHP regardless of user input.

That is incorrect. An unchecked checkbox field will not even exist in the POST data.

 

If you don't believe me, run this test page:

<html>
<body>
<form method="post">
Check/uncheck the checkbox and submit to see what is sent in POST data:
<input type="checkbox" name="foo" value="bar" /><br />
<button type="submit">Submit</button>
</form>
<br /><br />
Post Data:<br />
<pre>
<?php print_r($_POST); ?>
</pre>
</body>
</html>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.