Page 1 of 1

php form validation checkboxes

Posted: Wed Jan 14, 2009 8:30 pm
by clodagh2000
Hi I've created a form where i want the user to select 5 tick boxes. if they choose less than or more than five, an error message is to appear saying you have choose less than five, or more than five depending on which.

however it will only display the error message to say you have selected less than five tickboxes, even if i have selected 5, or even if i have selected more than five. can anyone help me out here? It would be much appreciated.

I have used the following code:

Code: Select all

 
<?php
 
      function VerifyForm(&$values, &$errors)
      {
         if($arraySize < 5){
            $errors['description'] = "you must choose 5 options.  You have selected less than 5 options.";
              
      }
      
      if($arraySize > 5)
      {
            $errors['description'] = "you must choose 5 options.  You have selected more than 5 options.";    
      }
      
    
      return (count($errors) == 0);
      }
      function DisplayForm($values, $errors)
      {
      ?>
      <html>
      <head>
      <title>Yadda yadda</title>
      <style>
      TD.error
      {
      color: red;
      font-weight: bold;
      }
      </style>
      </head>
      <body>
      <?php
      if (count($errors) > 0)
      echo "<p>There were some errors in your submitted form, please correct them and try again.</p>";
      ?>
       
      <form action="<?= $_SERVER['PHP_SELF'] ?>" method="POST">
      <table>
      <tr>
      <td colspan="2">Select 5 of the following options which best describe you:</td>
      
      </tr>
      <tr>
       <td class="error" colspan=2"><?= $errors['description'] ?></td>
  
  <table><tr>
  
      
<?php
in();
 
if ($db_found) 
{
    $SQL = "SELECT * FROM description";
    
    $result = mysql_query($SQL);
    while ($db_field = mysql_fetch_assoc($result)) 
    {
 
        $description = $db_field['description'];
        $des_id = $db_field['des_id'];
        $mod = $des_id % 3;
    
        if($mod == 0)
        {
            echo "<tr>";
        }               
                
        echo "<td><input type='checkbox' name='description[]' value='$description'/></td>";
        echo "<td>" . $description . "</td>";
        
        if($mod == 2)
        {
            echo "</tr>";   
        }
    }
    
mysql_close($db_handle);
 
}
else 
{
    
print "Database NOT Found ";
mysql_close($db_handle);
 
}
?>  
      
      
</table></td></tr>
      
      
      
     
      <tr><td colspan="2" align="center"><input type="submit" value="Submit"></tr>
      </table>
      </form>
       
      </body>
      </html>
      <?php
      }
       
      function ProcessForm($values)
      {
         
         
         
         
        $description = $_POST['description']; 
        $arraySize = count($description);
echo "<html><head><title>Thank you!</title></head><body>Thank you! 1 record added $arraySize this is the array size</body></html>";
      }
       
 
if ($_SERVER['REQUEST_METHOD'] == 'POST')
 
{
 
    $formValues = $_POST;
    $formErrors = array();
 
 
 
    if (!VerifyForm($formValues, $formErrors))
 
        DisplayForm($formValues, $formErrors);
 
    else
 
        ProcessForm($formValues);
 
}
else
 
DisplayForm(null, null);
?>  

Re: php form validation checkboxes

Posted: Wed Jan 14, 2009 10:18 pm
by novice4eva
You must have missed this

Code: Select all

 
function VerifyForm(&$values, &$errors)
{
$arraySize=count($values['description']);
....
 

Re: php form validation checkboxes

Posted: Thu Jan 15, 2009 8:42 am
by clodagh2000
Thank you very much. I really appreciate it :D It works.. yippeeeeeeeeeeeeeeeeeeeeee. I'm finally finished with this problem.


Cheers
:wink: