Simple server-side validation using php -
i have problem validation input , select fields. if inputs empty , click on submit warning "this field required" appears.
but when fill clientid input , selection box empty validation fail. or invers, when select box selected , input empty.
here code:
<!doctype html> <html> <head> <style> .error {color: #ff0000;} </style> </head> <body> <?php $nameerr = $katalogerr = $selectkatalog = ""; $kdn = ""; if ($_server["request_method"] == "post") { if (empty($_post["kdn"])) { $nameerr = "this field required"; } else { $kdn = test_input($_post["kdn"]); // check if kdn contains letters, numbers , whitespace if (!preg_match("/^[a-za-z0-9 ]*$/", $kdn)) { $nameerr = "only letters, numbers , white space allowed"; } } if($_post["selectkatalog"] == 'default'){ $katalogerr = "this field required"; } else { $selectkatalog = $_post["selectkatalog"]; } } function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?> <h2>order catalog</h2> <p><span class="error">* required field.</span></p> <form method="post" action="<?php echo htmlspecialchars($_server["php_self"]);?>"> clientid: <input type="text" name="kdn"> <span class="error">* <?php echo $nameerr;?></span> <br><br> catalog: <select name="selectkatalog"> <option value="default">bitte wählen:</option> <option value="catalog1">catalog1</option> <option value="catalog2">catalog2</option> <option value="catalog3">catalog3</option> </select> <span class="error">* <?php echo $katalogerr;?></span> <br><br> <input type="submit" name="submit" value="submit"> </form> <?php echo "<h2>your input:</h2>"; echo $kdn; echo "<br>"; echo $selectkatalog; ?> </body> </html>
wiki
Comments
Post a Comment