PHP Forms

In this PHP tutorial, you will learn all about the Forms and various types of Form Validations in PHP. We will discuss in detail creating the forms and validating those forms.

What are Forms in PHP?

As we all know PHP is a server-side scripting language that is used to create dynamic web pages. Forms are used to accept the data from the users and these collected data will be stored in the database or these data will be processed and the output will be provided to the users.  A form is a type of HTML tag that incorporates graphical user interface elements like input boxes, checkboxes, radio buttons, and so on. The form is defined by the <form>...</form> tags, while GUI components such as input are defined by form elements. The PHP GET and POST methods are used to transfer the data from the user to the server. The method will be defined in the HTML form tag. 

PHP : Html Form

Creating the form using the GET method

The GET method is used to collect data from the HTML form after it has been submitted. When a form uses the get method to send data, the data is available in the query string, therefore the values are not concealed. The superglobal array variable $_GET contains the values coming through the URL. As the get method sends data through the URL it has a limit of 2000 characters. The get method is only used to send the non-sensitive data to the server as it will be visible in the URL so it is not secure to transfer sensitive data like passwords and all

Example: registration.html


<!DOCTYPE html>
<html lang="en">
<head>
  <title>Registration</title>
  <style>
   .row {
    display: flex;
    justify-content: space-between;
    margin-bottom: 5px;
   }
   .txtBox {
    width: 200px;
   }
   .btns {
    display: flex;
    justify-content: center;
    margin-top: 10px;
   }
  </style>
 </head>
 <body>
  <div class="content" >
   <h2 >Registration Form</h2>
   <form acti method="get">
    <div class="row">
     <label for="fName">Fisrt Name: </label>
     <input type="text" name="fName" class="txtBox" id="fName" />
    </div>
    <div class="row">
     <label for="lName">Last Name: </label>
     <input type="text" name="lName" class="txtBox" id="lName" />
    </div>
    <div class="row">
     <label for="phone">Phone: </label>
     <input type="text" name="phone" class="txtBox" id="phone" />
    </div>
    <div class="row">
     <label for="dob">Date of Birth: </label>
     <input type="date" name="dob" class="txtBox" id="dob" />
    </div>
    <div class="row">
     <label for="adrs">Address: </label>
     <textarea
      name="adrs"
      id="adrs"
      class="txtBox"
      cols="30"
      rows="10"
     ></textarea>
    </div>
    <div class="btns">
     <input type="submit" class="btn" value="Next" name="submit" />
     <input type="reset" class="btn" value="Clear" />
    </div>
   </form>
  </div>
 </body>
</html>

Output:


PHP - Form

Example: registration_view.php


<!DOCTYPE html>
<html lang="en">
<head>     
<meta charset="UTF-8">  
<meta http-equiv="X-UA-Compatible"> 
<meta name="viewport">
    <title>Registration view</title>
</head>
<?php
if (isset($_GET['submit'])) {
    $fName = $_GET['fName']; 
    $lName = $_GET['lName'];  
    $dob = $_GET['dob'];     
    $ph= $_GET['ph'];
    $adrs = $_GET['adrs'];
    $fullName = $fName . " " . $lName;
}
?>
<body>
    <h2>The Entered Details are:</h2>
    <p>
        <strong>Full Name: </strong> <?php echo $fullName; ?><br>
        <strong>Phone Number: </strong><?php echo $ph; ?><br>
        <strong>Date of Birth: </strong><?php echo $dob; ?><br>
        <strong>Address: </strong><?php echo $adrs; ?>
    </p>
</body>
</html>

Output:


PHP - Form

Creating the form using POST method

POST method is used to collect data from the HTML form after it has been submitted. Because the data is not accessible in the query string when a form implements the method post to transfer data, security standards are maintained in this manner. Post requests are commonly used to submit forms with huge amounts of data, such as file uploads, picture uploads, login forms, registration forms, and so on. Because the data provided through the post request is not visible to the URL browser, it is safe. With a post request, you may submit a significant quantity of datal

Example: registration.html


<!DOCTYPE html>
<html lang="en">
 <head>
  <meta charset="UTF-8" />   <meta http-equiv="X-UA-Compatible" c />   <meta name="viewport" c />
  <title>Registration</title>
  <style>
   .row {
    display: flex;
    justify-content: space-between;
    margin-bottom: 5px;
   }
   .txtBox {
    width: 200px;
   }
   .btns {
    display: flex;
    justify-content: center;
    margin-top: 10px;
   }
  </style>
 </head>
 <body>   <div class="content" >    <h2 >Registration Form</h2>      
<form action =" registration_view.php " method="post">
    <div class="row">
     <label for="fName">Fisrt Name: </label>
     <input type="text" name="fName" class="txtBox" id="fName" />
    </div>
    <div class="row">
     <label for="lName">Last Name: </label>
     <input type="text" name="lName" class="txtBox" id="lName" />
    </div>
    <div class="row">
     <label for="phone">Phone: </label>
     <input type="text" name="phone" class="txtBox" id="phone" />
    </div>
    <div class="row">
     <label for="dob">Date of Birth: </label>
     <input type="date" name="dob" class="txtBox" id="dob" />
    </div>
    <div class="row">
     <label for="adrs">Address: </label>
     <textarea
      name="adrs" class="txtBox"
      cols="30"
      rows="10"
     ></textarea>
    </div>
    <div class="btns">
     <input type="submit" class="btn" value="Next" name="submit" />
     <input type="reset" class="btn" value="Clear" />
    </div>
   </form>
  </div>
 </body>
</html>



Output:


PHP - Form

Example: registration_view.php


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" >
    <meta name="viewport" >
    <title>Registration view</title>
</head>
<?php
if (isset($_POST['submit'])) {
    $fName = $_POST['fName'];
    $lName = $_POST['lName'];
    $ph = $_POST['ph'];
    $dob = $_POST['dob'];
    $adrs = $_POST['adrs'];
    $fullName = $fName . " " . $lName;
}
?>
<body>
    <h2>The Entered Details are:</h2>
    <p>
        <strong>Full Name: </strong> <?php echo $fullName; ?><br>
        <strong>Phone Number: </strong><?php echo $ph; ?><br>
        <strong>Date of Birth: </strong><?php echo $dob; ?><br>
        <strong>Address: </strong><?php echo $adrs; ?>
    </p>
</body>
</html>

Output:


PHP - Form

Difference between GET and POST method in PHP

GET POST

Get requests are limited in the amount of data they can send because the data is encapsulated in the header.

In post requests, huge amounts of data can be sent because the body contains the data.

The get request is not safe since the data is visible in the URL.

The post request is safe since the data is not visible in the URL.

The get request can be bookmarked.

The post request can’t be bookmarked.

The get request is idempotent. This signifies that the second request will be ignored until the first request's response is received.

The post request is not idempotent

 

What are Form Validations in PHP?

Validation refers to the process of verifying the user's input. In PHP, two forms of validation are accessible. They are listed below:
Client-Side Validation: Validation is conducted on web browsers on client machines.
Server-Side Validation: After data is uploaded, it is transmitted to a server, which performs validation tests on the server computer.

Various validations rules applied on the fields

Field Validation Rule

Name

Should only contain letters and white space

Email

Should contain ‘@’ and ‘.’

Website

Should only contain a valid URL

Radio

Should select at least one

Check Box

Should select at least one

Server-side validations

How to check whether the URL is valid or not?

To check whether the entered URL is valid or not we check for the characters belonging to the valid keywords such as https, ftp, www, a-z, 0-9,..etc..


$website = input($_POST["site"]);
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
   $websiteErr = "Invalid URL"; 
}

How to check whether the Email is valid or not?

To check whether the entered Email is valid or not we check for the characters and check that the email contains ‘@’ and ‘.’ in it.


$email = input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
   $emailErr = "Invalid format and please re-enter valid email"; 
}

Output:


PHP - Form

Check whether the field in the form is valid or not?

Example: registration.php


<!DOCTYPE html>
<html lang="en">
<head>
 <title>Registration</title>
 <style>
  .row {
   display: flex;
   justify-content: space-between;
   margin-bottom: 5px;
  }
  .txtBox {
   width: 200px;
  }

  .btns {
   display: flex;
   justify-content: center;
   margin-top: 10px;
  }
  .error {
   color: red
  }
 </style>
</head>
<div class="row">
    <label for="lName">Last Name: </label>
    <span class="error"> <?php echo $lNameErr; ?></span>
    <input type="text" name="lName" class="txtBox" id="lName" />

   </div>
   <div class="row">
    <label for="email">Email: </label>
    <span class="error"> <?php echo $emailErr; ?></span>
    <input type="text" name="email" class="txtBox" id="email" />

   </div>
   <div class="row">
    <label for="phone">Phone: </label>
    <input type="text" name="phone" class="txtBox" id="phone" />
   </div>
   <div class="row">
    <label for="adrs">Address: </label>
    <textarea name="adrs" id="adrs" class="txtBox" cols="30" rows="10"></textarea>
   </div>
   <div class="btns">
    <input type="submit" class="btn" value="Next" name="submit" />
    <input type="reset" class="btn" value="Clear" />
   </div>
  </form>
 </div>
 <?php

 if ($_SERVER["REQUEST_METHOD"] == "POST") {
  echo "<h2>Your given values are as:</h2>";
  echo "<p>Full Name: $fName $lName <br> Email: $email <br> Phone: $phone <br> Address: $adrs </p>";
 }

 function test_input($data)
 {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
 }

 ?>

</body>
</html>

Output:


PHP - Form

PHP - Form