How to validate your HTML form through php?

In this example I printed the output on the same page under the form to show you the result. But you can print it in another page.

<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Form validation</title>
<style>
.error{color:#F00; font-family:Arial, Helvetica, sans-serif; font-size:16px;}
</style>
</head>
<body>
<?php
if(isset($_REQUEST['submit']))
{
    if(empty($_REQUEST['uname']))
    {
        $nameerror="Name is required";
    }
    
    else
    {
        $uname= test($_REQUEST['uname']);
        if(!preg_match("/^[a-zA-Z]*$/",$uname))
            {
                $nameerror="Only letters and white spaces are allowed";
            }
        else
        {
            $nameerror="";
        }
    }
    
    if(empty($_REQUEST['email']))
    {
        $emailerror="Email is required please fill it";
    }
    else
    {
        $email=test($_REQUEST['email']);
        if(!filter_var($email, FILTER_VALIDATE_EMAIL))
        {
            $emailerror="Invalid Email Format";
        }
    }
    
    if(empty($_REQUEST['website']))
    {
        $website="";
    }
    else
    {
        $website=test($_REQUEST['website']);
        if(!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",
        $website))
        {
            $websiteerror="Invalid Website Format. Please fill the correct format";
        }
    }
    
    if(empty($_REQUEST['gender']))
    {
        $gendererror="Please select your gender";
    }
    else
    {
        $gender=test($_REQUEST['gender']);
    }
}

function test($data) {
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}

?>
<span class="error">* <u>Required Fields</u> </span><br/><br/>
<form method="post" action="form_validation.php">
<table cellpadding="10px">
    <tr>
        <td>Name: </td>
        <td><input type="text" name="uname"/><span class="error"> *<?php echo $nameerror ?></span></td>
    </tr>
    <tr>
        <td>Email: </td>
        <td><input type="email" name="email"/><span class="error"> *<?php echo $emailerror ?></span></td>
    </tr>
    <tr>
        <td>Website: </td>
        <td><input type="text" name="website"/><span class="error"><?php echo $websiteerror ?></span></td>
    </tr>
    <tr>
        <td>Gender: </td>
        <td><input type="radio" name="gender" value="Male"/>Male
            <input type="radio" name="gender" value="Female"/>Female<span class="error"> *<?php echo $gendererror ?></span>
        </td> 
    </tr>
    <tr>
        <td>Comment: </td>
        <td><textarea name="comment"></textarea>
    </tr>
    <tr>
        <td>&nbsp;</td>
        <td align="right"><input type="submit" name="submit" value="Submit"/></td>
    </tr>
</table>
</form>

<?php
echo "<h3> Your details are given below: </h3>"."<br/>";
echo $uname."<br/>";
echo $email."<br/>";
echo $website."<br/>";
echo $gender."<br/>";
echo $comment;
?>
</body>
</html>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s