Day3 with PHP | Form Validation using with bootstrap3 (for testing only)


<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<body>
<?php
$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;
}
?>
<div class="container">
<h3>PHP Form Validation Example</h3><hr>
<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>">
<div class="form-group">
<label for="name">Name</label>
<input type="text" name="name" class="form-control" placeholder="name" required>
</div>
<div class="form-group">
<label for="email">E-mail</label>
<input type="text" name="email" class="form-control" placeholder="email" required>
</div>
<div class="form-group">
<label for="website">Website</label>
<input type="text" name="website" class="form-control" placeholder="website" required>
</div>
<div class="form-group">
<label for="comment">Comment</label>
<textarea name="comment" class="form-control"></textarea>
</div>
<div class="radio">
<label><input type="radio" class="radio" name="gender" value="female">Female</label>
<label><input type="radio" class="radio" name="gender" value="male">Male</label>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>

<?php 
echo "<h3>Your input:</h3><hr>";
echo $name."<br>";
echo $email."<br>";
echo $website."<br>";
echo $comment."<br>";
echo $gender;
?>
</div>
</body>
</html>

________________________


<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<?php
$nameErr = $emailErr = $websiteErr = $genderErr = "";
$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;
}
?>
<div class="container">
<h3>PHP Form Validation Example</h3><hr>
<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>">
<div class="form-group">
<label for="name">Name</label>
<input type="text" name="name" class="form-control" placeholder="name">
<span class="error">*<?php echo $nameErr;?></span>
</div>
<div class="form-group">
<label for="email">E-mail</label>
<input type="text" name="email" class="form-control" placeholder="email">
<span class="error">*<?php echo $emailErr;?></span>
</div>
<div class="form-group">
<label for="website">Website</label>
<input type="text" name="website" class="form-control" placeholder="website">
<span class="error"><?php echo $websiteErr;?></span>
</div>
<div class="form-group">
<label for="comment">Comment</label>
<textarea name="comment" class="form-control"></textarea>
</div>
<div class="radio">
<label><input type="radio" class="radio" name="gender" value="female">Female</label>
<label><input type="radio" class="radio" name="gender" value="male">Male</label>
<span class="error">*<?php echo $genderErr;?></span>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>

<?php 
echo "<h3>Your input:</h3><hr>";
echo $name."<br>";
echo $email."<br>";
echo $website."<br>";
echo $comment."<br>";
echo $gender;
?>
</div>
</body>
</html>


_________________________

You can check the validation like this as well!

<?php
$nameErr = $emailErr = $websiteErr = $genderErr = "";
$name = $email = $gender = $comment = $website ="";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "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"])) {
$emailErr = "Email is required";
}else{
$email = test_input($_POST["email"]);
//check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr ="Invalid email format";
}
}
if (empty($_POST["website"])) {
$website ="";
}else{
$website = test_input($_POST["website"]);
//check if URL address syntax is valid (this regular expression also allows dashes in the URL)
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $website)) {
$websiteErr = "Invalid URL";
}
}
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;
}

?>





Previous Post Next Post