Form Handling In PHP

PHP gives the developer a variety of ways to handle and control the forms built using HTML. The data from the HTML form can be sent to the PHP scripts using either the $_GET or $_POST. There is nothing extraordinary about building the forms controlled by PHP scripts.

The form can be built by just using the basic tags of HTML. For example, see the following code:

<html>
<body>
<form action="actionscript.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>

When the user clicks on the submit button, the page action.php is called which handles the data accordingly as per the code.

See another example below which is a real-life use of PHP scripts:

<html>
<head>
<title>Registration Form</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<h2>Registration Form</h2>
<form action="form.php" method="POST"> 
First name:
<input type="text" name="firstname"><br>
 Last name:
<input type="text" name="lastname"> <br>
E-mail: <input type="text" name="email"><br>
<input type="hidden" name="form_submitted" value="1"/>
<input type="submit" value="Submit">
</form>
</body>
</html>

The above form is designed for taking the firstname,  lastname and email from the user. The details are sent to the form.php using the POST method.


GET vs POST

It should be noted that POST  and GET both create and implement array internally in the key-value pair system.

Both GET and POST is treated as $_GET and $_POST. These are superglobals, which means that they are always accessible, regardless of scope – and you can access them from any function, class or file without having to do anything special.

$_GET is an array of variables passed to the current script via the URL parameters.

$_POST is an array of variables passed to the current script via the HTTP POST method.

Note: 1. One should use GET when user deals with non-sensitive data since the data is passed as a part of the URL. The data that can be sent must be limited.

2. The POST method must be used when a user deals with sensitive data. In this method, the data is not passed as a part of the URL. Also, there is no limit on the size of the data.

There are various ways in which the data received from the HTML forms can be used and manipulated using such scripts.


FORM – REQUIRED FIELDS

Sometimes the page demands some data that must be entered by the user. Such fields are known as required fields. These fields data can be checked using PHP.

The data can be checked for its validity and value that it contains. The validity of the data can be checked using functions like preg_match(), filter_val() and etc.

If the data received is empty, that case can be checked by using the empty() function by passing the parameter that is to be checked.

For a better understanding look at the following example:

<?php
// define variables and set to empty values
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $website = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (empty($_POST["name"])) {
    $nameErr = "Name is required";
  } else {
    $name = check($_POST["name"]);
  }

  if (empty($_POST["email"])) {
    $emailErr = "Email is required";
  } else {
    $email = check($_POST["email"]);
  }

  if (empty($_POST["website"])) {
    $website = "";
  } else {
    $website = check($_POST["website"]);
  }

    if (empty($_POST["gender"])) {
    $genderErr = "Gender is required";
  } else {
    $gender = check($_POST["gender"]);
  }
}
?>

The above script checks whether the data received from the form i.e, name, website Url, email and gender is empty or not.

If the data is not empty then the form is accepted otherwise an error message is displayed.

Leave a Comment

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