How To Use PHP Form Validation Example

PHP 8 , PHP

Nicesnippets

557

14-06-2021


How To Use PHP Form Validation Example

Hi guys,

Today i will explained how to use php form validation use in your form . This example is so easy to use in php. This example to i am a create a bootstrap form and set to the validation in your form input.

This example to i am create a name,email,website,comment,and gender field and set to the validation in php.

So let's start to the example.

Example :


form.php

<?php

$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;

}

?>

<!DOCTYPE html>

<html lang="en">

<head>

<title>How To Use PHP Form Validation Example</title>

<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>

<style>

.error {

color: #FF0000;

}

</style>

</head>

<body>

<div class="container">

<h2>How To Use PHP Form Validation Example</h2>

<p><span class="error">* required field</span></p>

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

<div class="form-group">

<label for="name">Name:<span class="error">*</span></label>

<input type="text" class="form-control" id="name" placeholder="Enter Name" name="name">

<span class="error"><?php echo $nameErr;?></span>

</div>

<div class="form-group">

<label for="email">Email:<span class="error">*</span></label>

<input type="email" class="form-control" id="email" placeholder="Enter email" name="email">

<span class="error"><?php echo $emailErr;?></span>

</div>

<div class="form-group">

<label for="website">Website:</label>

<input type="website" class="form-control" id="website" placeholder="Enter Website" name="website">

<span class="error"><?php echo $websiteErr;?></span>

</div>

<div class="form-group">

<label>Comment:</label>

<textarea name="comment" class="form-control" rows="5" cols="40"></textarea>

</div>

<label>Gender:<span class="error">*</span></label><br>

<label class="radio-inline">

<input type="radio" name="gender" value="male"> Male

</label>

<label class="radio-inline">

<input type="radio" name="gender" value="female"> Female

</label>

<label class="radio-inline">

<input type="radio" name="gender" value="other"> Other

</label>

<span class="error">* <?php echo $genderErr;?></span>

<br>

<button type="submit" class="btn btn-primary">Submit</button>

</form>

</div>

<div class="container mt-3 mb-3">

<div class="row">

<div class="col-md-12">

<?php

echo "<h2>Your Output:</h2>";

echo $name;

echo "<br>";

echo $email;

echo "<br>";

echo $website;

echo "<br>";

echo $comment;

echo "<br>";

echo $gender;

?>

</div>

</div>

</div>

</body>

</html>

Output

Your Output:

Evelyn Mason

[email protected]

abc.com

how are you

male

now you can check your own.

i hope it can help you...


Recommended Posts