Forms can be validated with JavaScript and HTML5, but it ois adviced to perform ultimate rewview of submitted data with PHP script (or other server) and prevent harmful database updates.

Examples below show a few ways of PHP based forms evaluation after submitting:

Example of $_POST form submission with letter, number, email and URL filters

<?php
    // variables preset
    $nameval = $ageval = $emailval = $urlval = "";
    $name = $age = $email = $url = "";
    
    // check submitted method
    if ($_SERVER["REQUEST_METHOD"] == "POST")
    {
        // check NAME (letters)
        if (empty($_POST["name"]))
        {
            $nameval = "Name is required";
        }
        else
        {
            $name = test_input($_POST["name"]);
            if (!preg_match("/^[a-zA-Z]*$/", $name)) // validating string
            {
                $nameval = "Only letters allowed";
            }
        }
        
        // check AGE (number)
        if (empty($_POST["age"]))
        {
            $ageval = "Age is required";
        }
        else
        {
            $age = test_input($_POST["age"]);
            if (!preg_match("/^[0-9][0-9]*$/", $age)) // validating numbers
            {
                $ageval = "Only numbers allowed";
            }
        }
        
        // check EMAIL (email format)
        if (empty($_POST["email"]))
        {
            $emailval = "Email is required";
        }
        else
        {
            $email = test_input($_POST["email"]);
            if (!filter_var($email, FILTER_VALIDATE_EMAIL)) // validating email
            {
                $ageval = "Only emails allowed";
            }
        }
        
        // check WEBSITE (url format)
        if (empty($_POST["url"]))
        {
            $urlval = "URL is required";
        }
        else
        {
            $url = test_input($_POST["url"]);
            if (!preg_match("/\b(?:https?|ftp):\/\/|www\.[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $url)) // validating URL
            {
                $urlval = "Only proper URL allowed";
            }
        }
        
    }
?>

 

›› go to examples ››