Hi everyone, this time we will discuss about PHP Validation using the Required Field. Not many people know, it turns out that the application of required fields in PHP is very easy and practical. Without waiting too long, this section tells the best way to make input fields required and make blunder messages if necessary. Please listen and don’t forget to put it into practice on your application. PHP Forms – Validation With Required Fields.
PHP – Required Fields
From the approval rules table on the past page, we see that the “Name”, “Email”, and “Gender” fields are required. These fields can’t be vacant and must be rounded out in the HTML structure.
Field | Validation Rules |
---|---|
Name | Required. + Must only contain letters and whitespace |
Required. + Must contain a valid email address (with @ and .) | |
Website | Optional. If present, it must contain a valid URL |
Comment | Optional. Multi-line input field (textarea) |
Gender | Required. Must select one |
PHP Forms – Validation With Required Fields
In the accompanying code we have included some new factors: $nameErr, $emailErr, $genderErr, and $websiteErr. These mistake factors will hold blunder messages for the necessary fields. We have additionally included an if else articulation for each $_POST variable. This checks if the $_POST variable is vacant (with the PHP unfilled() work). On the off chance that it is vacant, a blunder message is put away in the distinctive mistake factors, and in the event that it isn’t unfilled, it sends the client input information through the test_input() work:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?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"]); } } ?> |
PHP – Display The Error Messages
At that point in the HTML structure, we include a little content after each necessary field, which creates the right blunder message if necessary (that is if the client attempts to present the structure without rounding out the necessary fields):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> <div class="form-group"> <label>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>Email :</label> <input type="text" name="email" class="form-control" placeholder="Email"> <span class="error">* <?php echo $emailErr;?></span> </div> <div class="form-group"> <label>Website :</label> <input type="text" name="website" class="form-control" placholder="Website"> <span class="error"><?php echo $websiteErr;?></span> </div> <div class="form-group"> <label>Comments :</label> <textarea name="comment" class="form-control" placeholder="Comments..." rows="5" cols="40"></textarea> </div> <div class="form-group"> <label>Gender :</label> <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> </div> <div class="form-group"> <input type="submit" name="submit" value="Submit"> </div> </form> |
The following stage is to approve the info information, that is “Does the Name field contain just letters and whitespace?”, and “Does the E-mail field contain a substantial email address language structure?”, and whenever rounded out, “Does the Website field contain a legitimate URL?”.
[the_ad id=”651″]