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).

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

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().

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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.