Detri Amelia Chandra
  • HOME
  • GIS
  • HTML
  • Bootstrap
  • CSS
  • Javascript
  • Ubuntu
  • Useful Tools
    • URL Redirect Tool
    • URL Download Encryptor
No Result
View All Result
Detriamelia.com
  • HOME
  • GIS
  • HTML
  • Bootstrap
  • CSS
  • Javascript
  • Ubuntu
  • Useful Tools
    • URL Redirect Tool
    • URL Download Encryptor
No Result
View All Result
Detriamelia.com
No Result
View All Result

14. PHP Forms – Validation With Required Fields

15/02/2020
in PHP
PHP Forms - Validation With Required Fields

PHP Forms - Validation With Required Fields

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
E-mail 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

PHP Forms - Validation With Required Fields
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″]

Live Demo Download Source Code
Tags: PHP
ShareTweetShare

Related Posts

PHP Form Handling
PHP

13. New Performing PHP Form Handling

Detriamelia.Com - Running form functions in PHP is not difficult. By using the $ _POST and $ _GET methods it...

13/02/2020
Learn PHP Global Variables - Superglobals With Example
PHP

12. Learn PHP Global Variables – Superglobals With Example

On this occasion, we will discuss Global and Superglobal Variables. These variables are some that are very important to implement....

20/01/2020
PHP Array Example, Complete Editions
PHP

11. PHP Array Example, Complete Editions

Welcome back to the PHP programming learning page. This time we will discuss arrays, more precisely arrays in PHP. Arrays...

17/01/2020
PHP Function Definition and Example
PHP

10. PHP Function Definition and Example

In this discussion, we will discuss about PHP Function. We really need PHP Function in certain circumstances when coding. The...

15/01/2020
Next Post
Web and Mobile Programming – Library Part-1

Web and Mobile Programming - Library Part-1

Database Table Relations Using Join Operations and the Classical Model

Database Table Relations Using Join Operations and the Classical Model

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recommended

Learn PHP Global Variables - Superglobals With Example

12. Learn PHP Global Variables – Superglobals With Example

20/01/2020
Beautify Bootstrap Modal Animation With Animate.CSS

Beautify Bootstrap Modal Animation With Animate.CSS

02/04/2019
Easy to Identify Identity Card Number (NIK) Using PHP

Easy to Identify Identity Card Number (NIK) Using PHP

27/03/2019
PHP Syntax, Variables and PHP Echo/Print

5. PHP Echo and Print Statements

29/11/2019

Instagram Feed

    The Instagram Access Token is expired, Go to the Customizer > JNews : Social, Like & View > Instagram Feed Setting, to refresh it.
  • HOME
  • GIS
  • HTML
  • Bootstrap
  • CSS
  • Javascript
  • Ubuntu
  • Useful Tools
Detriamelia.Com

© 2022 Detri Amelia Chandra - IT Tips and Programming Tutorials.

No Result
View All Result
  • HOME
  • GIS
  • HTML
  • Bootstrap
  • CSS
  • Javascript
  • Ubuntu
  • Useful Tools
    • URL Redirect Tool
    • URL Download Encryptor