arfa Posted September 21, 2006 Share Posted September 21, 2006 I want to test if various checkboxes have been checked - or, more importantly, to report an error if left unchecked.code example:[code]if(isset($_POST['checker'])) { foreach ($_POST as $name => $value) { echo "name=$name & value=$value<BR>"; if(empty($value)) {echo "empty $name<BR>";} if(is_string($value)) {echo "string $name<BR>";}}}echo "<BR><BR><form action=check.php method=POST> <input type=checkbox name=chk_box> check this<BR> <input type=text name=another> another<BR><input type=submit name=checker value=submit></form>";[/code]The (empty) text input prints the name and can be tested as empty but unless the checkbox is checked the box is 'invisible'. The checkbox is either 'on' or 'off' so I test for is_string.Same result. Even the empty text input still registers as a string, but the checkbox... nothing.I suspect I am missing one of those 'obvious when you know' bits of info.Any pointers to the obvious, or even obscure, are most welcome. Quote Link to comment Share on other sites More sharing options...
warewolfe Posted September 22, 2006 Share Posted September 22, 2006 Hej, I'm not certain if I understood your question, but should you just check to see if the checkbox is "off" That is if(chck_box == "off") { //Error message } else if(chck_box =="on" { //Everything okay message } else { //violation of excluded middle law message } Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted September 22, 2006 Share Posted September 22, 2006 The problem here is that the value for checkboxes isn't sent to your script if it is not checked. If these must always be checked, you probably should be using radio buttons as an on/off toggle. That way you will be sure to get either a value when the form is submitted.Ken Quote Link to comment Share on other sites More sharing options...
arfa Posted September 22, 2006 Author Share Posted September 22, 2006 yes, radio buttons would work if there was an either-or option but the idea with the checkbox is a multiple choice. In my case two out of five.I am not sure but wouldn't if(chk_box == "off") generate an error.What is chk_box? - undefined variable?and $_POST['chk_box'] doesn't register.thanks - arfa 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.