English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Basic PHP Tutorial

Advanced PHP Tutorial

PHP & MySQL

PHP Reference Manual

PHP Form Handling

In this tutorial, you will learn how to use PHP superglobal variables $_GET, $_POST, and $_REQUEST to collect user input submitted through the form.

Create a simple contact form

In this tutorial, we will create a simple HTML contact form that allows users to enter their comments and feedback, which is then displayed in the browser using PHP.

Open your favorite code editor and create a new PHP file. Now enter the following code and save this file as 'contact'.-form.php" is located in the root directory of the project.

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8>
    <title>Contact Form</title>
</head>
    <h2>Contact Us</h2>
    <p>Please fill out this form and send it to us.</p>
    <form action="process-form.html" 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">Content:<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>
</html>

Usage of the code

Note that there are two attributes in the beginning of the <form> tag:

  • The 'action' attribute refers to a PHP file named 'process'.-The file 'form.php' receives the data entered into the form when the user submits the form by pressing the 'Submit' button.

  • The method attribute tells the browser toPOST Methodsend form data.

The remaining elements within the form are basic form controls used to receive user input. To learn more about HTML form elements, please seeHTML FormTutorial.

Using PHP to Capture Form Data

To access the value of a specific form field, you can use the following superglobal variables. These variables are available in all scopes of the script.

SuperglobalsDescription
$_GETcontaining a list of all field names and values sent with the form using the GET method (i.e., through URL parameters).
$_POSTcontaining a list of all field names and values sent with the form using the POST method (data is not visible in the URL).
$_REQUESTcontaining the values of the $_GET and $_POST variables as well as the values of the $_COOKIE superglobal variables.

When the user submits the above contact form by clicking the "Submit" button, the form data will be sent to the " process-form.php" file for processing. It only captures the information submitted by the user and displays it to the browser.

“ process-The PHP code for the "form.php" file is as follows:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8>
    <title>Contact Form</title>
</head>
    <h1>Thank you</h1>
    <p>Here is the information you submitted:</p>
    <ol>
        <li><em>Name:</em> <?php echo $_POST["name"]?></li>
        <li><em>Email:</em> <?php echo $_POST["email"]?></li>
        <li><em>Subject:</em> <?php echo $_POST["subject"]?></li>
        <li><em>Message:</em> <?php echo $_POST["message"]?></li>
    </ol>
</html>

The above PHP code is very simple. Since the form data is sent using the POST method, you can retrieve the value of a specific form field by passing the name of the form field to the $_POST superglobal array and display the value of each field using the echo() statement.

In the real world, you cannot trust user input. You must implement some validation to filter user input before using it. In the next chapter, you will learn how to clean and validate this contact form data and use PHP to send it via email.