How do I know if a checkbox has been clicked upon sending a formail? Something like the user will check a box if they want a copy of their message to be sent to them also. I'm not sure if this is a PHP question or a JavaScript one but if it's a JS one then my apologies.
If it haven't been checked the variable $_POST['checkboxname'] will not be set. You can check that with: if (isset($_POST['checkboxname'])) { echo 'Its set'; } else { echo 'Its not set'; }
It's javascript if it happens before the form gets submitted to the php script, I believe I understand you and it seems you did post it to the right section. All you do is just check if the value of the box is equal to 'on' so... <?php if ($_POST['is_checked'] == 'on') { echo 'The box is checked.<br /><br />' ; } ?> <form action="" method="post"> <input type="checkbox" name="is_checked"> <input type="submit" </form> PHP: For some reason, checking to see if the box is not checked does not work like if ($_POST['is_checked'] == 'off') PHP: Perhaps someone else can help with this part of it as I would just do if ($_POST['is_checked'] != 'on') PHP: which works perfectly fine. Hope this helps
Good thing I posted it here then. So the string which is returned is 'on' I initially thought it would return a boolean value. I remember trying it and failing miserably. So, if there are multiple checkboxes you just have to go through each one, right? So if it's 'off' it doesn't return any value at all...that's weird.
If it doesn't return any value then it's pretty much easier to go through php <?php if(isset($_POST['checkboxname'])) { //it was checked! } ?> Code (markup): Btw how come my code isn't color coded unlike yours? I'm jealous.
Yes, that is bizarre and I really didn't realize that until reading tamen's post (he checks to see if the variable is set at all). It kind of sounds wrong to have a field in a form and it not return any value at all if it's not checked. Ooh well, that's programmer's logic for you.
You could always install Firefox and download the LiveHTTPHeaders plugin and see what exactly gets send when you submit a form.
Tamen, I dropped by the Firefox add-ons page. Would you suggest Header Spy 1.2 or Header Monitor 0.3? I'd rather use what you've used/are using.
I use the one called Live HTTP Headers. You can find it here: http://livehttpheaders.mozdev.org/ Its not pretty or anything and it will show you what gets send in the headers of any request made by Firefox. A godsend when you are automating anything on a webpage. Say, writing an automated Wordpress-account creator