php form validation 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
clodagh2000
Forum Newbie
Posts: 4
Joined: Fri Aug 10, 2007 9:46 am

php form validation checkboxes

Post 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);
?>  
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

Re: php form validation checkboxes

Post by novice4eva »

You must have missed this

Code: Select all

 
function VerifyForm(&$values, &$errors)
{
$arraySize=count($values['description']);
....
 
clodagh2000
Forum Newbie
Posts: 4
Joined: Fri Aug 10, 2007 9:46 am

Re: php form validation checkboxes

Post by clodagh2000 »

Thank you very much. I really appreciate it :D It works.. yippeeeeeeeeeeeeeeeeeeeeee. I'm finally finished with this problem.


Cheers
:wink:
Post Reply