jayhawker Posted March 29, 2011 Share Posted March 29, 2011 What is the best way using php to see if a checkbox is checked or not. I see so many different options out there googling, that I am interested knowing the most robust method. Thanks Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 29, 2011 Share Posted March 29, 2011 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. Quote Link to comment Share on other sites More sharing options...
Skewled Posted March 29, 2011 Share Posted March 29, 2011 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. Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 29, 2011 Share Posted March 29, 2011 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). Quote Link to comment Share on other sites More sharing options...
Skewled Posted March 29, 2011 Share Posted March 29, 2011 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! Quote Link to comment Share on other sites More sharing options...
DJTim666 Posted March 29, 2011 Share Posted March 29, 2011 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(). Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 30, 2011 Share Posted March 30, 2011 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> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.