Everything You Need To Know About PHP Form Handling

Posted by TotalDC

We have already talked about what $_GET and $_POST superglobal variables are. In this tutorial, you will learn about PHP form handling, and how to collect user data from inputs using PHP superglobal variables. We are going to create a simple HTML contact form that will allow users to enter their comments and feedback and then display it in the browser using PHP.

How To Create A Simple Contact Form In PHP

To create an HTML contact form, open up your favorite code editor and create a new PHP file. Then type the following code and save the file as “contact-form.php” in the root directory of your project.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Contact Form</title>
</head>
<body>
    <h2>Contact Us</h2>
    <form action="process-form.php" method="POST">
        <p>
            <label for="inputName">Name:<sup>*</sup></label>
            <input type="text" name="name" id="inputName">
        </p>
        <p>
            <label for="inputEmail">Email:<sup>*</sup></label>
            <input type="text" name="email" id="inputEmail">
        </p>
        <p>
            <label for="inputSubject">Subject:</label>
            <input type="text" name="subject" id="inputSubject">
        </p>
        <p>
            <label for="inputComment">Message:<sup>*</sup></label>
            <textarea name="message" id="inputComment" rows="5" cols="30"></textarea>
        </p>
        <input type="submit" value="Submit">
        <input type="reset" value="Reset">
    </form>
</body>
</html>

As you can see, there are two attributes within the opening <form> tag. The action attribute references a PHP file “process-form.php” that receives the data entered into the form when the user presses the submit button. And the method attribute tells the browser to send the data through the POST method.

Other elements inside the form are basic form controls to receive user inputs. If you are new to this, you can learn more about HTML form elements here.

Capturing Form Data In PHP

To access the value of a certain from field you can use the following superglobal variables These are available in all scopes throughout a script.

  • $_GET – Contains a list of all the field names and values sent by a form using the get method (i.e. via the URL parameters).
  • $_POST – Contains a list of all the field names and values sent by a form using the post method (data will not be visible in the URL).
  • $_REQUEST – Contains the values of both the $_GET and $_POST variables as well as the values of the $_COOKIE superglobal variable.

When the user submits the above form by clicking the submit button, data from the form is sent to the “process-form.php” file on the server for processing. Then data is captured and displayed on your screen.

The PHP code of the “process-form.php” file will look like this:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Contact Form</title>
</head>
<body>
    <h1>Thank you for submitting your info</h1>
    <ol>
        <li><em>Name:</em> <?php print $_POST["name"]?></li>
        <li><em>Email:</em> <?php print $_POST["email"]?></li>
        <li><em>Subject:</em> <?php print $_POST["subject"]?></li>
        <li><em>Message:</em> <?php print $_POST["message"]?></li>
    </ol>
</body>
</html>

As you can see, you can retrieve the value of a particular form field by passing its name to the $_POST superglobal and display each field value by using a print() statement.

In real real-world scenario, you can’t trust the user inputs and must implement some sort of validation to filter the user inputs before doing something else with them. You will learn how to validate data that you collected from inputs in the next article.