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

14. PHP Forms – Validation With Required Fields

Detri Amelia Chandra by Detri Amelia Chandra
15/02/2020
in PHP
0
PHP Forms - Validation With Required Fields

PHP Forms - Validation With Required Fields

0
SHARES
37
VIEWS
Share on FacebookShare on Twitter

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.

Recommended Post

14. PHP Forms – Validation With Required Fields

13. New Performing PHP Form Handling

12. Learn PHP Global Variables – Superglobals With Example

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
Previous Post

13. New Performing PHP Form Handling

Next Post

Pemrograman Web dan Perangkat Bergerak – Perpustakaan Part-1

Related Posts

PHP Forms - Validation With Required Fields

14. PHP Forms – Validation With Required Fields

15/02/2020
PHP Form Handling

13. New Performing PHP Form Handling

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

12. Learn PHP Global Variables – Superglobals With Example

20/01/2020
PHP Array Example, Complete Editions

11. PHP Array Example, Complete Editions

17/01/2020
PHP Function Definition and Example

10. PHP Function Definition and Example

15/01/2020
PHP Loop (While, For, Foreach) Definition and Example

9. PHP Loop (While, For, Foreach) Definition and Example

15/01/2020
Next Post
PEMROGRAMAN WEB DAN PERANGKAT BERGERAK – PERPUSTAKAAN PART-1

Pemrograman Web dan Perangkat Bergerak - Perpustakaan Part-1

Tinggalkan Balasan Batalkan balasan

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *

Top Stories

Relasi Tabel Database Menggunakan Operasi Join dan Model Klasik

Relasi Tabel Database Menggunakan Operasi Join dan Model Klasik

30/04/2020
PEMROGRAMAN WEB DAN PERANGKAT BERGERAK – PERPUSTAKAAN PART-1

Pemrograman Web dan Perangkat Bergerak – Perpustakaan Part-1

16/04/2020
PEMROGRAMAN WEB DAN PERANGKAT BERGERAK – PERPUSTAKAAN PART-1

Pemrograman Web dan Perangkat Bergerak – Perpustakaan Part-1

16/04/2020

News & More

Categories

  • Counseling
  • Consultations
  • Relationship
  • Friendship
  • Singlehood

About Us

We bring you the best Premium WordPress Themes that perfect for news, magazine, personal blog, etc. Check our landing page for details.

Connect on Social

© 2019 JNews - Premium WordPress news & magazine theme by Jegtheme.

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

© 2021 Detri Amelia Chandra - IT Tips and Programming Tutorial.