Jump to content

Archived

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

arfa

checkbox ghost - check emptiness

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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
  }

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

×

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.