Jump to content

checkbox ghost - check emptiness


arfa

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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