Wednesday 15 August 2012

How to validate an input form using php -



How to validate an input form using php -

i have created form follows need validate user input using php. security measure should not rely on javascript/ html 5 form validation validate form submissions. should employ server side validation verify info beingness submitted write php code following: 1. validate firstname, lastname , email required 2. validate age if entered number 3. validate email , website entries ensure valid

<!doctype html> <html> <head> <title>page title</title> </head> <?php $firstname=""; $lastname=""; $email=""; $age=""; $website=""; if ($_server["request_method"] == "post") { if (empty($_post["firstname"])) { $firstname = "first name required"; } else { $firstname = test_input($_post["firstname"]); } if (empty($_post["lastname"])) { $lastname = "last name required"; } else { $lastname = test_input($_post["lastname"]); } if (empty($_post["email"])) { $email = "email required"; } else { $email = test_input($_post["email"]); } if (is_numeric ($_post["age"])) {} else { $age ="age must numeric"; } } echo $firstname; echo $lastname; echo $email; echo $age; ?> <form action="." method="post"> <input type="text" name="firstname" placeholder="*first name" /><br> <input type="text" name="lastname" placeholder="*last name" /><br> <input type="text" name="email" placeholder="*email" /><br> <input type="text" name="age" placeholder="age" /><br> <input type="text" "name="website" placeholder="website" /><br> <input type="submit" name="submit" value="submit" /> </form> <body> </body> </html>

so looks this:

here, form happen have in scripts library, , can modify suit needs.

strangely enough, has function called test_input() , wanted achieve.

sidenote: sure alter own $myemail = "email@example.com";

<?php ob_start(); ?> <!doctype html public "-//w3c//dtd xhtml 1.0 strict//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-strict.dtd"> <style> .error {color: #ff0000;} h6 { font-family: bookman old style; font-size:20px; text-align: center; font-weight: normal; } h5 { font-family: bookman old style; font-size:15px; text-align: center; font-weight: normal; } </style> <?php $nameerr = $emailerr = $websiteerr = $commenterr = $categoryerr = ""; $name = $email = $comment = $website = $category = ""; if ($_server["request_method"] == "post") { if (empty($_post["name"])) { $nameerr = "name required"; $err = 1; } else { $name = test_input($_post["name"]); if (!preg_match("/^[a-za-z ]*$/",$name)) { $nameerr = "only letters , white space allowed"; } } if (empty($_post["email"])) { $emailerr = "email required"; $err = 1; } else { $email = test_input($_post["email"]); if (!filter_var($email, filter_validate_email)) { $emailerr = "invalid email format"; $err = 1; // die(); } } if (empty($_post["website"])) { $websiteerr = "url required"; $err = 1; } else { $website = test_input($_post["website"]); if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) { $websiteerr = "invalid url"; } } if (empty($_post["comment"])) { // $comment = ""; $commenterr = "comment required"; $err = 1; } else { $comment = test_input($_post["comment"]); } // if (empty($_post["category"])) { if ($_post["category"] == "" ) { $categoryerr = "category required"; $err = 1; } else { $category = test_input($_post["category"]); } } function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); homecoming $data; } ?> <?php echo htmlspecialchars($_server["php_self"]);?> <h6>link submission</h6> <h5><span class="error">* required field.</span> <form method="post" action="<?php echo htmlspecialchars($_server["php_self"]);?>"> name of site: <input type="text" name="name" value="<?php echo $name;?>"> <span class="error">* <?php echo $nameerr;?></span> <br><br> e-mail: <input type="text" name="email" value="<?php echo $email;?>"> <span class="error">* <?php echo $emailerr;?></span> <br><br> url: <input type="text" name="website" value="<?php echo $website;?>"> <span class="error">* <?php echo $websiteerr;?></span> <br><br> description: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea><span class="error">* <br><?php echo $commenterr;?></span> <br><br> category of site: <select size="1" name="category"> <option value="<?php echo $category;?>"> -- please select -- </option> <option>arts</option> <option>business</option> <option>computers</option> <option>games</option> <option>health</option> <option>home</option> <option>kids , teens</option> <option>news</option> <option>recreation</option> <option>reference</option> <option>science</option> <option>shopping</option> <option>society</option> <option>sports</option> <option>world</option> </select><span class="error">* <?php echo $categoryerr;?></span> <br><br> <input type="submit" name="submit" value="submit"> </form> </h5> <?php if(isset($_post['submit'])){ if ($err != 1){ $myemail = "email@example.com"; $subject = "link submission"; $message = "your link submission form has been submitted by: website name: $name e-mail: $email url: $website category: $category description: $comment"; $headers = "from: ". $name . " <" . $email . ">\r\n"; mail($myemail, $subject, $message, $headers); // header('location: submit_thanks.php'); echo "ok"; } } ?>

php forms validation

No comments:

Post a Comment