PHP Form Handling

PHP Form Handling | In this tutorial you are going to learn how to handle the forms data used in PHP using $_GET, $_POST, and $_REQUEST superglobal variable.


Whenever we are developing any web application or website, we ofter create different forms like user login form or registration form to receive data from the user.

These forms can be created using pure HTML and CSS. However, PHP is used to send and handle the data collected from the form to the server.

PHP provides three superglobal variables $_GET, $_POST, $_REQUEST to accomplish this task.

Creating a Simple HTML Form

The first thing is to create a simple HTML form to receive the inputs from the user and display it to the browser.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Contact Form</title>
</head>
<body>
    <h2>Contact Form</h2>
    <p>Please fill in this form and send us.</p>
    <form action="form-process.php" method="post">
        <p>
            <label for="inputName">First Name:<sup>*</sup></label>
            <input type="text" name="firstname" id="inputFirstName">
        </p>
        <p>
            <label for="inputName">Last Name:<sup>*</sup></label>
            <input type="text" name="lastname" id="inputLastName">
        </p>
        <p>
            <label for="inputEmail">Email:<sup>*</sup></label>
            <input type="text" name="email" id="inputEmail">
        </p>
         <input type="submit" value="Submit">
    </form>
</body>
</html>

In the above example, we have used <form> tag to create an HTML form with input fields for First Name, Last Name, and Email. In the <form>, two attributes action and method have been used. These are described below.

  • action – This specifies the action to be taken after submitting the form. In our case, we are passing the control to the form-process.php to handle the data.
  • method – This attribute specifies the method of sending form-data to the sever. There is two methods are available – GET and POST.

You can also use get method instead of post method in the above example.

The form will look likes as below.

basic html form


PHP Form Handling with GET

If we specify “get” in the HTML form,  then the form-data is sent to the server using the HTTP GET method.

To process the form-data which has been sent through HTTP GET method, PHP has superglobal variable $_GET to handle the data.

Example

File: form-process.php

<?php

// getting the value of firstname and lastname field
$firstname = $_GET["firstname"];
$lastname = $_GET["lastname"];
// getting the value of the email field
$email = $_GET["email"];

echo "Hi, ". $firstname . $lastname . "<br>";
echo "Your email address is: ". $email ."<br>";

?>

In the above example, we used $_GET superglabal array to extract data like firstname, lastname, email from the form. Later on, we have printed the data using echo() function.

Output

Hi Sagar Sharma
Your email address is: sagarsharma@gmail.com

PHP form Handling with POST

In case we are using “post” method in the HTML form, then the form-data is sent to the server using the HTTP POST method.

To process the form-data which has been sent through the HTTP POST method, PHP has superglobal variable $_POST to handle the data.

Example

File: form-process.php

<?php

// getting the value of firstname and lastname field
$firstname = $_POST["firstname"];
$lastname = $_POST["lastname"];
// getting the value of the email field
$email = $_POST["email"];

echo "Hi, ". $firstname . $lastname . "<br>";
echo "Your email address is: ". $email ."<br>";

?>

Output

Hi Sagar Sharma
Your email address is: sagarsharma@gmail.com

PHP form Handling with $_REQUEST

In the previous sections, we have seen  that we need to use $_GET for the method “get” and $_POST for the method “post“. There is another way to process the form data in PHP. We can use $_REQUEST superglobal variable to process the form-data. $_REQUEST can be used for both “get” and “post” method.

Example

File: form-process.php

<?php

// getting the value of firstname and lastname field
$firstname = $_REQUEST["firstname"];
$lastname = $_REQUEST["lastname"];
// getting the value of the email field
$email = $_REQUEST["email"];

echo "Hi, ". $firstname . $lastname . "<br>";
echo "Your email address is: ". $email ."<br>";

?>

Output

Hi Sagar Sharma
Your email address is: sagarsharma@gmail.com

Please get connected & share!

Advertisement