0% found this document useful (0 votes)
7 views12 pages

Sanitization Validation Programs

The document provides multiple examples of PHP forms with sanitization and validation for user inputs such as name, email, address, and gender. Each example demonstrates different levels of validation, including required fields and error messages for invalid inputs. The forms utilize a function to sanitize inputs by trimming whitespace, removing slashes, and converting special characters to HTML entities.

Uploaded by

Ankepalli Mahesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views12 pages

Sanitization Validation Programs

The document provides multiple examples of PHP forms with sanitization and validation for user inputs such as name, email, address, and gender. Each example demonstrates different levels of validation, including required fields and error messages for invalid inputs. The forms utilize a function to sanitize inputs by trimming whitespace, removing slashes, and converting special characters to HTML entities.

Uploaded by

Ankepalli Mahesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Sanitization/ Validation programs

Example1

<!DOCTYPE HTML>
<html>
<head>
</head>
<body>

<?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;
}
?>

<h2>PHP Form Validation Example</h2>


<form method="post" action="<?php echo
htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name: <input type="text" name="name">
<br><br>
E-mail: <input type="text" name="email">
<br><br>
Website: <input type="text" name="website">
<br><br>
Comment: <textarea name="comment" rows="5" cols="40"></textarea>
<br><br>
Gender:
<input type="radio" name="gender" value="female">Female
<input type="radio" name="gender" value="male">Male
<input type="radio" name="gender" value="other">Other
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
echo "<h2>Your Input:</h2>";
echo $name;
echo "<br>";
echo $email;
echo "<br>";
echo $website;
echo "<br>";
echo $comment;
echo "<br>";
echo $gender;
?>

</body>
</html>

Example2

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

<?php
// define variables and set to empty values
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
}

if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
}

if (empty($_POST["website"])) {
$website = "";
} else {
$website = test_input($_POST["website"]);
}

if (empty($_POST["comment"])) {
$comment = "";
} else {
$comment = test_input($_POST["comment"]);
}

if (empty($_POST["gender"])) {
$genderErr = "Gender is required";
} else {
$gender = test_input($_POST["gender"]);
}
}

function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>

<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="name">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
E-mail: <input type="text" name="email">
<span class="error">* <?php echo $emailErr;?></span>
<br><br>
Website: <input type="text" name="website">
<span class="error"><?php echo $websiteErr;?></span>
<br><br>
Comment: <textarea name="comment" rows="5" cols="40"></textarea>
<br><br>
Gender:
<input type="radio" name="gender" value="female">Female
<input type="radio" name="gender" value="male">Male
<input type="radio" name="gender" value="other">Other
<span class="error">* <?php echo $genderErr;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
echo "<h2>Your Input:</h2>";
echo $name;
echo "<br>";
echo $email;
echo "<br>";
echo $website;
echo "<br>";
echo $comment;
echo "<br>";
echo $gender;
?>

</body>
</html>

Example3

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

<?php
// define variables and set to empty values
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
}

if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
}

if (empty($_POST["website"])) {
$website = "";
} else {
$website = test_input($_POST["website"]);
}

if (empty($_POST["comment"])) {
$comment = "";
} else {
$comment = test_input($_POST["comment"]);
}

if (empty($_POST["gender"])) {
$genderErr = "Gender is required";
} else {
$gender = test_input($_POST["gender"]);
}
}

function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>

<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="name" value="<?php echo $name;?>">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
E-mail: <input type="text" name="email" value="<?php echo $email;?>">
<span class="error">* <?php echo $emailErr;?></span>
<br><br>
Website: <input type="text" name="website" value="<?php echo
$website;?>">
<span class="error"><?php echo $websiteErr;?></span>
<br><br>
Comment: <textarea name="comment" rows="5" cols="40"><?php echo
$comment;?></textarea>
<br><br>
Gender:
<input type="radio" name="gender" value="female" <?php if
(isset($gender) && $gender=="female") echo "checked";?>>Female
<input type="radio" name="gender" value="male" <?php if
(isset($gender) && $gender=="male") echo "checked";?>>Male
<input type="radio" name="gender" value="other" <?php if
(isset($gender) && $gender=="other") echo "checked";?>>Other
<span class="error">* <?php echo $genderErr;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>

<?php
echo "<h2>Your Input:</h2>";
echo $name;
echo "<br>";
echo $email;
echo "<br>";
echo $website;
echo "<br>";
echo $comment;
echo "<br>";
echo $gender;
?>

</body>
</html>

Example4

<?php
$name = $email = $address = $gender = "";

if($_SERVER['REQUEST_METHOD'] == 'POST')
{
$name = test($_POST["name"]);
$email = test($_POST["email"]);
$address = test($_POST["address"]);
$gender = test($_POST["gender"]);
}
function test($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"])?>"


method="POST">
Name:<input type="text" name="name"> <br><br>
Email:<input type="text" name="email"><br><br>
Address: <textarea name="address" cols="30"
rows="10"></textarea><br><br>
Gender: <input type="radio" name="gender" value="male"> male
<input type="radio" name="gender" value="female"> female
<br><br>
<input type="submit">
</form>

<?php
echo "<h1>The data filled includes the following:</h1>";
echo $name. "<br>";
echo $email. "<br>";
echo $address. "<br>";
echo $gender. "<br>";
?>

Example5

<?php
$name = $email = $address = $gender = "";
$error_name = $error_email = $error_address = $error_gender = "";

if($_SERVER['REQUEST_METHOD']=='POST')
{
if(empty($_POST['name']))
{
$error_name="Name is mandatory field";
}
else
{
$name = test($_POST['name']);
}

if(empty($_POST['email']))
{
$error_email="Email is mandatory field";
}
else
{
$email = test($_POST['email']);
}

if(empty($_POST['address']))
{
$error_address="Address is mandatory field";
}
else
{
$address = test($_POST['address']);
}

if(empty($_POST['gender']))
{
$error_gender="Gender is mandatory field";
}
else
{
$gender = test($_POST['gender']);
}

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

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"])?>"


method="POST">
Name:<input type="text" name="name"> <span><?php echo $error_name; ?
></span><br><br>
Email:<input type="text" name="email"><span><?php echo $error_email; ?
></span><br><br>
Address: <textarea name="address" cols="30"
rows="10"></textarea><span><?php echo $error_address; ?></span><br><br>
Gender: <input type="radio" name="gender" value="male"> male
<input type="radio" name="gender" value="female"> female
<span><?php echo $error_gender; ?></span>
<br><br>
<input type="submit">
</form>

<?php
echo "<h1>The data filled includes the following:</h1>";
echo $name. "<br>";
echo $email. "<br>";
echo $address. "<br>";
echo $gender. "<br>";
?>
Example6

<?php
$name = $email = $address = $gender = "";
$error_name = $error_email = $error_address = $error_gender = "";

if($_SERVER['REQUEST_METHOD']=='POST')
{
if(empty($_POST['name']))
{
$error_name="Name is mandatory field";
}
else
{
$name = test($_POST['name']);
}

if(empty($_POST['email']))
{
$error_email="Email is mandatory field";
}
else
{
$email = test($_POST['email']);
}

if(empty($_POST['address']))
{
$error_address="Address is mandatory field";
}
else
{
$address = test($_POST['address']);
}

if(empty($_POST['gender']))
{
$error_gender="Gender is mandatory field";
}
else
{
$gender = test($_POST['gender']);
}

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

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"])?>"


method="POST">
Name:<input type="text" name="name" value="<?php echo $name;?>">
<span><?php echo $error_name; ?></span><br><br>
Email:<input type="text" name="email" value="<?php echo $email;?
>"><span><?php echo $error_email; ?></span><br><br>
Address: <textarea name="address" cols="30" rows="10"><?php echo
$address;?></textarea><span><?php echo $error_address; ?
></span><br><br>
Gender: <input type="radio" name="gender" value="male" <?php if
(isset($gender) && $gender=="female") echo "checked";?>> male
<input type="radio" name="gender" value="female" <?php if
(isset($gender) && $gender=="male") echo "checked";?>> female
<span><?php echo $error_gender; ?></span>
<br><br>
<input type="submit">
</form>

<?php
echo "<h1>The data filled includes the following:</h1>";
echo $name. "<br>";
echo $email. "<br>";
echo $address. "<br>";
echo $gender. "<br>";
?>

Example7

<?php
$name = $email = $address = $gender = "";
$error_name = $error_email = $error_address = $error_gender = "";

if($_SERVER['REQUEST_METHOD']=='POST')
{
if(empty($_POST['name']))
{
$error_name="Name is mandatory field";
}
else
{
$name = test($_POST['name']);
}

if(empty($_POST['email']))
{
$error_email="Email is mandatory field";
}
else
{
$email = test($_POST['email']);
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false)
{
$error_email="";
}
else
{
$error_email="Not a valid email";
}
}

if(empty($_POST['address']))
{
$error_address="Address is mandatory field";
}
else
{
$address = test($_POST['address']);
}

if(empty($_POST['gender']))
{
$error_gender="Gender is mandatory field";
}
else
{
$gender = test($_POST['gender']);
}
}

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

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"])?>"


method="POST">
Name:<input type="text" name="name" value="<?php echo $name;?>">
<span><?php echo $error_name; ?></span><br><br>
Email:<input type="text" name="email" value="<?php echo $email;?
>"><span><?php echo $error_email; ?></span><br><br>
Address: <textarea name="address" cols="30" rows="10"><?php echo
$address;?></textarea><span><?php echo $error_address; ?
></span><br><br>
Gender: <input type="radio" name="gender" value="male" <?php if
(isset($gender) && $gender=="female") echo "checked";?>> male
<input type="radio" name="gender" value="female" <?php if
(isset($gender) && $gender=="male") echo "checked";?>> female
<span><?php echo $error_gender; ?></span>
<br><br>
<input type="submit">
</form>

<?php
echo "<h1>The data filled includes the following:</h1>";
echo $name. "<br>";
echo $email. "<br>";
echo $address. "<br>";
echo $gender. "<br>";
?>

You might also like