I have made a script that checks if content is entered in the html boxes and if the required fields are empty, their border lights red.
This all works, but if I try to make a required field of a drop down box I can't get it working -_-
Expand|Select|Wrap|Line Numbers
- <html>
- <head>
- <style type="text/css">
- .forgotten {
- border: 1px solid #ff0000;
- }
- </style>
- </head>
- <body>
- <?php
- //----- function to check required fields
- function CheckRequired($array)
- {
- //----- check if $array is not empty
- if (!empty($array))
- {
- //----- loop through the array and check if the value's been set
- foreach ($array as $value)
- {
- if (empty($_POST[$value]))
- $form_error[] = $value;
- }
- }
- //--- return an array with missed fields
- return $form_error;
- }
- //----- check for required fields
- if ($_SERVER['REQUEST_METHOD'] == "POST")
- $required_fields = CheckRequired(array('title', 'omschrijving'));
- //----- check if all required fields are entered
- if ($_SERVER['REQUEST_METHOD'] == "POST" && empty($required_fields))
- {
- echo "Verwerk formulier<br>";
- echo "<u>Categorie:</u> " . $_POST['categorie'] . "<br>";
- echo "<u>Title:</u> " . $_POST['title'] . "<br>";
- echo "<u>Omschrijving:</u> " . $_POST['omschrijving'] . "<br>";
- }
- else
- {
- //----- if form's been posted before, there are some fields left undone
- if (!empty($required_fields))
- {
- $i = 1;
- foreach ($required_fields as $value)
- {
- // echo "U forgot the field: " . ucfirst($value) . ".<br />";
- $varname = "class_" . $value;
- ${$varname} = "class=forgotten";
- $i++;
- }
- }
- echo "<form name=\"form\" method=\"POST\" action=\"" . $_SERVER['PHP_SELF'] . "\">";
- echo "<table>";
- echo "<tr>";
- echo "<td>Categorie:</td>";
- echo "<td><select name=\"categorie\">";
- echo "<option value=\"0\">Kies</option>";
- echo "<optgroup label=\"HTML\">";
- echo "<option value=\"1\">HTML</option>";
- echo "</optgroup>";
- echo "<optgroup label=\"CSS\">";
- echo "<option value=\"2\">CSS</option>";
- echo "</optgroup>";
- echo "<optgroup label=\"PHP\">";
- echo "<option value=\"3\">PHP</option>";
- echo "<option value=\"4\">Databases</option>";
- echo "<option value=\"5\">OOP</option>";
- echo "</optgroup>";
- echo "<optgroup label=\"Design\">";
- echo "<option value=\"6\">Photoshop</option>";
- echo "</optgroup>";
- echo "<optgroup label=\"Programma's\">";
- echo "<option value=\"7\">Programma's</option>";
- echo "</optgroup>";
- echo "<optgroup label=\"Besturingssystemen\">";
- echo "<option value=\"8\">Linux</option>";
- echo "<option value=\"9\">Windows</option>";
- echo "<option value=\"10\">Mac</option>";
- echo "</optgroup>";
- echo "</select>";
- echo "</td>";
- echo "</tr>";
- echo "<tr>";
- echo "<td>Titel:</td>";
- echo "<td><input type=\"text\" name=\"title\" width=\"50\" maxlength=\"60\" value=\"" . htmlentities($_POST['title']) . "\" " . $class_title . " /><br></td>";
- echo "</tr>";
- echo "<tr>";
- echo "<td>Omschrijving (inleiding): </td>";
- echo "<td><textarea rows=\"5\" cols=\"50\" name=\"omschrijving\" value=\"" . htmlentities($_POST['omschrijving']) . "\" " . $class_omschrijving . " /></textarea></td>";
- echo "</tr>";
- echo "<tr><td><input type=\"submit\" name=\"submit\" value=\"Verzenden\"><td></tr>";
- echo "</table>";
- echo "</form>";
- }
- ?>
- </body>
- </html>
Greetings,
Reinhout