Hello, welcome to zhullyblog. If this is your first time here , please
Subscribe to Zhullyblog by Email to get latest tutorial.
How to make a form mandatory
This tutorial teaches you to make input field mandatory and create error message if the user leaves the field empty.
There are times when you need to make a particular place in your form compulsory. These means that the field must be filled and cannot be left empty.
What are the compulsory field that must be filled in a form?
There are several field that must be filled depending on your form. But most times, the Name , password , Email are compulsory to be filled.
Let’s go into details why you need to make some compulsory.
•
Name
The name must contain letters and space. In reality, every one has a name in letters, you could specify that the first letter must be in capital letter.
•
Username
The username can have both letters and numbers.
•
Email
The Email must contain a valid image with @.
•
Comment
It is actually optional but you can decide to make it compulsory.
All I am saying is - once you have declared a field compulsory , the user will have to fill it and if the user doesn't , an error message will be displayed.
The mandatory field is actually very important when you need some important information from user.
Okay, let’s go straight to the code.
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>
Name: <input type="text" name="name" placeholder="Name">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
E-mail:
<input type="text" name="email" placeholder="Email">
<span class="error">* <?php echo $emailErr;?></span>
<br><br>
Website:
<input type="text" name="website" placeholder="Website">
<span class="error"><?php echo $websiteErr;?></span>
<br><br>
Comment: <textarea name="comment" rows="5" cols="40" placeholder="Comment"></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>
Screenshot
Output
Okay, let’s break it down.
The
$nameErr , $emailErr , $websiteErr will hold the error message for the mandatory field.
Now , we need to display an error message when the user fail to fill the form and click on submit without filling the form.
Here is the code :
let’s go straight to the code.
<?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 = "Please enter your name";
} else {
$name = test_input($_POST["name"]);
}
if (empty($_POST["email"])) {
$emailErr = "Please enter your email address";
} 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"]);
}
}
?>
Waw, you are done with PHP forms. Let's move straight to advance PHP. Meet you there.
Follow us on
Please share
Comments
Post a Comment