Requiring at least one checkbox to be checked??

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
sunnydayz
Forum Newbie
Posts: 6
Joined: Mon Feb 13, 2006 9:16 pm

Requiring at least one checkbox to be checked??

Post by sunnydayz »

How do I require at least one checkbox to be checked. I have a form with text fields and checkboxes that is emailed.

Here is the html for the checkbox section...

Code: Select all

<td height="24"> 
              <input name="checkbox1" type="checkbox" id="checkbox1" value="checkbox1">
              Checkbox 1 </td>
          </tr>
          <tr>
            <td align="right" valign="middle">&nbsp;</td>
            <td><input name="checkbox2" type="checkbox" id="checkbox2" value="checkbox2">
              Checkbox 2</td>
          </tr>
          <tr>
            <td align="right" valign="middle">&nbsp;</td>
            <td><input name="checkbox3" type="checkbox" id="checkbox3" value="checkbox3">
              Checkbox 3</td>
          </tr>
          <tr>
            <td align="right" valign="middle">&nbsp;</td>
            <td><input name="checkbox4" type="checkbox" id="checkbox4" value="checkbox4">
              Checkbox 4</td>

Here is the php for my mail code...

Code: Select all

<?php
   // check to see if a name was entered
   if (!$_POST['First_Name'])
      // if not, add that error to our array
      $errors[] = "First name is required";
   if (!$_POST['Last_Name'])
      // if not, add that error to our array
      $errors[] = "Last name is required";
   // if there are any errors, display them
   if (count($errors)>0){
      echo "<strong>ERROR:<br>\n";
      foreach($errors as $err)
        echo "$err<br>\n";
   } else {
      // no errors, so we build our message
	  
		 $first_name = stripslashes($_POST['First_Name']);
		 $last_name = stripslashes($_POST['Last_Name']);
		 $address = ($_POST['Address']);
		 $city = ($_POST['City']);
		 $state = ($_POST['State']);
		 $zip = ($_POST['ZIP_Code']);
		 $phone = ($_POST['Phone']);
		 $email = stripslashes($_POST['Email']);
         $checkbox1=($_POST['checkbox1'])?"Yes":"No";
		 $checkbox2=($_POST['checkbox2'])?"Yes":"No";
		 $checkbox3=($_POST['checkbox3'])?"Yes":"No";
		 $checkbox4=($_POST['checkbox4'])?"Yes":"No";
		 $msg.="\n$checkbox1\n$checkbox2\n$checkbox3\n$checkbox4";
		 $comments = stripslashes($_POST['MsgBody']);

//the mailing part
$to = 'email@gmail.com';
$subject = 'Email Form';
$msg = "Name: $first_name $last_name\n\n"."Address: $address\n\n".
		"City: $city\n\n"."State: $state\n\n"
		."ZIP Code: $zip\n\n"."Phone: $phone\n\n"."Email: $email\n\n"
		."Checkbox 1: $checkbox1\n\n"."Checkbox 2: $checkbox2\n\n"
		."Checkbox 3: $checkbox3\n\n"."Checkbox 4: $checkbox4\n\n"
		."Comments: $comments\n\n";
;
		

if (mail($to, $subject, $msg)){
            echo "<p>Form Sent!</p>";
            echo nl2br($msg);
         } else
            echo "An unknown error occurred."; 
}
?>
What code do I use for this and where to I put it in the process.php?

Thanks!
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post by matthijs »

Maybe something like:

Code: Select all

<?php
if (isset($_POST['submit'])) {

  if( !is_array($_POST['checkbox']) )
  {
     echo "please fill in at least one checkbox";
  }
  else
  {
     foreach($_POST['checkbox'] as $value)
    {
       echo "$value"; // or do something else ..
    }
  }

}
?>
<form action="checkboxes.php" method="post">
Check1 <input type="checkbox" name="checkbox[]" value="checkbox1" ><br>
Check2 <input type="checkbox" name="checkbox[]" value="checkbox2" ><br>
Check3 <input type="checkbox" name="checkbox[]" value="checkbox3" ><br>
Check4 <input type="checkbox" name="checkbox[]" value="checkbox4" ><br>
<input type="submit" value="Submit" name="submit">
</form>
Post Reply