Error checking those evil checkboxes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sillywilly
Forum Newbie
Posts: 19
Joined: Thu May 02, 2002 5:11 pm

Error checking those evil checkboxes

Post by sillywilly »

I have the following code:

Code: Select all

I don't have a computer/console
<input type="checkbox" name="S1Q3_1" value="0"><br>
Playing interactive games is unsociable
<input type="checkbox" name="S1Q3_2" value="0"><br>
Playing interactive games is boring
<input type="checkbox" name="S1Q3_3" value="0"><br>
Playing interactive games is geeky
<input type="checkbox" name="S1Q3_4" value="0"><br>
They're expensive
<input type="checkbox" name="S1Q3_5" value="0"><br>
I have never had the opportunity
<input type="checkbox" name="S1Q3_6" value="0"><br>
I don't have the time
<input type="checkbox" name="S1Q3_7" value="0"><br>
Other
<input type="checkbox" name="S1Q3_8" value="0"><br>
I need someway to check that at least one of the checkboxes is checked.

Radio buttons are easy as they are all the same name, thus i use:

Code: Select all

// check whether input is empty
	function isEmpty($field, $msg)
	{
		$value = $this->_getValue($field);
		if (trim($value) == "")
		{
			$this->_errorList&#1111;] = array("field" => $field, "value" => $value, "msg" => $msg);
			return false;
		}
		else
		{
			return true;
		}
	}
So with this i can have this:

(S1Q1 being the name of the radio buttons)

Code: Select all

$fv->isEmpty("S1Q1", "You have not selected whether you play games or not");
 
if ($fv->isError())
{
        $errors = $fv ->getErrorList();
echo "<br>The operation could not be preformed because one or more error(s) occu
rred.</br>";
 
        echo "<ul>";
        foreach ($errors as $e)
        {
                echo "<li>" . $e&#1111;'msg'];
        }
        echo "</ul>";
echo '<br>Please go  <a href="#" onClick="history.go(-1)">Back</a>  and try agai
n</br>';
}
Which would see if a value was passed. But in this situation the check boxes all have their own name thus a simple check like this cannot be done.

I need a way to check that at least one of the check boxes have been checked.

Any suggestion..... I'm not sure where to begin.....i'm just a baby when it comes to PHP.

Thanks for your help in advance.
dusty
Forum Contributor
Posts: 122
Joined: Sun Apr 28, 2002 9:52 pm
Location: Portsmouth, VA

Post by dusty »

put your checkboxes in an array ie:
<input type="checkbox" name="check[0]">
<input type="checkbox" name="check[1]">

then you can check if check is set.

Code: Select all

if(!isset($_POST&#1111;'check'])) &#123;
  do errors;
&#125;
sillywilly
Forum Newbie
Posts: 19
Joined: Thu May 02, 2002 5:11 pm

Post by sillywilly »

The problem with this is that i have the following which prints out form data passed as hidden variables:

Code: Select all

<?php
$excludeus = array("Submit", "stage");
while (list($k, $v) = each($HTTP_POST_VARS))
&#123;
   if( !in_array($k, $excludeus) )&#123;
      echo "<input type=hidden name="".$k."" value="".htmlspecialchars($v)."">
";
   &#125;
&#125;
Thus meaning that the names and the values will not be printed as hidden variables on the next form now. Thats why i named them different in the first place so that all the variables would print up.

Also how would i construct an mysql insert statement to add the values of the check boxes now as they all have the same name?

What i had planned was have a rows called:

S1Q3_1
S1Q3_2
S1Q3_3

How will this now work with the code you suggested?

Do you understand what i am trying to say?
dusty
Forum Contributor
Posts: 122
Joined: Sun Apr 28, 2002 9:52 pm
Location: Portsmouth, VA

Post by dusty »

you would use the name, the same as any other post var. ie: $check[0] would hold the value of <input name="check[0]">
sillywilly
Forum Newbie
Posts: 19
Joined: Thu May 02, 2002 5:11 pm

Post by sillywilly »

This does not seem to work for:

Code: Select all

<?php 
$excludeus = array("Submit", "stage"); 
while (list($k, $v) = each($HTTP_POST_VARS)) 
&#123; 
   if( !in_array($k, $excludeus) )&#123; 
      echo "<input type=hidden name="".$k."" value="".htmlspecialchars($v)."">
"; 
   &#125; 
&#125;
The aboce code only prints:

: <input type=hidden name="S1Q3" value="Array">
dusty
Forum Contributor
Posts: 122
Joined: Sun Apr 28, 2002 9:52 pm
Location: Portsmouth, VA

Post by dusty »

i can think of a couple ways to go about it, but i don't think it would be pretty. i'd have to see exactly what you were doing to figure out a nice way to do it.

maybe it's just because it's 2:26am and my brain isn't functioning..8O

err. just saw your post on devshed, i'll look at the code you posted there tomorrow though. :wink:
sillywilly
Forum Newbie
Posts: 19
Joined: Thu May 02, 2002 5:11 pm

Post by sillywilly »

Way cool, thank you very much...
Post Reply