Skip to content

Instantly share code, notes, and snippets.

@calebcgates
Created July 26, 2016 14:46
Show Gist options
  • Save calebcgates/45e6c06352db1f1e2e0d4c6370ae7bc6 to your computer and use it in GitHub Desktop.
Save calebcgates/45e6c06352db1f1e2e0d4c6370ae7bc6 to your computer and use it in GitHub Desktop.
PHP test_input
<?php
// define variables and set to empty values
$name = $email = $gender = $comment = $website = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = test_input($_POST["name"]);
$email = test_input($_POST["email"]);
$website = test_input($_POST["website"]);
$comment = test_input($_POST["comment"]);
$gender = test_input($_POST["gender"]);
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
@yusuf-saif
Copy link

yusuf-saif commented Sep 24, 2021

if ($_SERVER["REQUEST_METHOD"] == "POST") {
        if (empty($_POST["name"])) {
          $name_err = "Name is required";
        } else {
          $name = test_input($_POST["name"]);
          // check if name only contains letters and whitespace
          if (!preg_match("/^[a-zA-Z-' ]*$/",$name)) {
            $nameErr = "Only letters and white space allowed";
          }
        }
        
        if (empty($_POST["email"])) {
          $email_err = "Email is required";
        } else {
          $email = test_input($_POST["email"]);
          // check if e-mail address is well-formed
          if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $email_err= "Invalid email format";
          }
        }

here is a long way in solving it also.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment