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);
?>