×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: PHP
Posted by: Dylan Lopez
Added: Nov 23, 2017 9:55 AM
Views: 2
Tags: no tags
  1. // define variables and set to empty values
  2. $nameErr = $emailErr = $genderErr = $websiteErr = "";
  3. $name = $email = $gender = $comment = $website = "";
  4.  
  5. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  6.   if (empty($_POST["name"])) {
  7.     $nameErr = "Name is required";
  8.   } else {
  9.     $name = test_input($_POST["name"]);
  10.     // check if name only contains letters and whitespace
  11.     if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
  12.       $nameErr = "Only letters and white space allowed";
  13.     }
  14.   }
  15.  
  16.   if (empty($_POST["email"])) {
  17.     $emailErr = "Email is required";
  18.   } else {
  19.     $email = test_input($_POST["email"]);
  20.     // check if e-mail address is well-formed
  21.     if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  22.       $emailErr = "Invalid email format";
  23.     }
  24.   }
  25.  
  26.   if (empty($_POST["website"])) {
  27.     $website = "";
  28.   } else {
  29.     $website = test_input($_POST["website"]);
  30.     // check if URL address syntax is valid (this regular expression also allows dashes in the URL)
  31.     if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
  32.       $websiteErr = "Invalid URL";
  33.     }
  34.   }
  35.  
  36.   if (empty($_POST["comment"])) {
  37.     $comment = "";
  38.   } else {
  39.     $comment = test_input($_POST["comment"]);
  40.   }
  41.  
  42.   if (empty($_POST["gender"])) {
  43.     $genderErr = "Gender is required";
  44.   } else {
  45.     $gender = test_input($_POST["gender"]);
  46.   }
  47. }
  48.