Core PHP

Form Validation

This PHP example program demonstrates how to write and handle a simple form with validation.

FormWithValidation.php

<!DOCTYPE HTML>
<html>
<head>
<style>
  .error {color: #FF0000;}
</style>
</head>
<body>

<?php
// Initialize the variables
$sNameError = $sEmailError = $sGenderError = $sWebsiteError = "";
$sName = $sEmail = $sGender = $sComment = $sWebsite = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (empty($_POST["sNameKey"])) {
    $sNameError = "A name is required.";
  } else {
    $sName = Clean($_POST["sNameKey"]);
    // Make sure that the name contains only letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/", $sName)) {
      $sNameError = "Only letters and white space are allowed.";
    }
  }

  if (empty($_POST["sEmailKey"])) {
    $sEmailError = "An email address is required.";
  } else {
    $sEmail = Clean($_POST["sEmailKey"]);
    // Verify that the e-mail address is well-formed
    if (!filter_var($sEmail, FILTER_VALIDATE_EMAIL)) {
      $sEmailError = "Invalid email format.";
    }
  }

  if (empty($_POST["sWebsiteKey"])) {
    $sWebsite = "";
  } else {
    $sWebsite = Clean($_POST["sWebsiteKey"]);
    // Check whether the URL address syntax is valid. Dashes are allowed too.
    $sExpression1 = "/\b(?:(?:https?|ftp):\/\/|www\.)";
    $sExpression2 = "[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i";
    if (!preg_match($sExpression1.$sExpression2, $sWebsite)) {
      $sWebsiteError = "Invalid URL";
    }
  }

  if (empty($_POST["sCommentKey"])) {
    $sComment = "";
  } else {
    $sComment = Clean($_POST["sCommentKey"]);
  }

  if (empty($_POST["sGenderKey"])) {
    $sGenderError = "A gender is required.";
  } else {
    $sGender = Clean($_POST["sGenderKey"]);
  }
}

function Clean($sData) {
  $sData = trim($sData);
  $sData = stripslashes($sData);
  $sData = htmlspecialchars($sData);
  return $sData;
}
?>

<h2>PHP Form Validation Example</h2>
<p><span class="error">* required field.</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
  Name: <input type="text" name="sNameKey" value="<?php echo $sName;?>">
  <span class="error">* <?php echo $sNameError;?></span>
  <br /><br />
  E-mail: <input type="text" name="sEmailKey" value="<?php echo $sEmail;?>">
  <span class="error">* <?php echo $sEmailError;?></span>
  <br /><br />
  Website: <input type="text" name="sWebsiteKey" value="<?php echo $sWebsite;?>">
  <span class="error"><?php echo $sWebsiteError;?></span>
  <br /><br />
  Comment: <textarea name="sCommentKey" rows="5" cols="40"><?php echo $sComment;?></textarea>
  <br /><br />
  Gender:
  <input type="radio" name="sGenderKey"
    <?php if (isset($sGender) && $sGender=="female") echo "checked";?>
    value="female">Female
  <input type="radio" name="sGenderKey"
    <?php if (isset($sGender) && $sGender=="male") echo "checked";?>
    value="male">Male
  <span class="error">* <?php echo $sGenderError;?></span>
  <br /><br />
  <input type="submit" name="sSubmitKey" value="Submit">
</form>

<?php
  echo "<h2>Form Input:</h2>";
  echo $sName;
  echo "<br>";
  echo $sEmail;
  echo "<br>";
  echo $sWebsite;
  echo "<br>";
  echo $sComment;
  echo "<br>";
  echo $sGender;
?>
</body>
</html>
 

Output

 
 

© 2007–2025 XoaX.net LLC. All rights reserved.