PHP HTML form

Summary: in this tutorial, you will learn how an HTML form works and how to capture data from HTML form in PHP.

An HTML form consists of HTML input elements that allow you to enter the data and submit it to the web server. Those input elements include text input field, password field, checkbox, radio button, file select field, submit button…etc.

An HTML form begins with an opening <form> tag, and ends with a closing </form> tag as follows:

There are two important attributes of the <form> tag:

  • action: the action attribute accepts an absolute URL ( http://localhost/phpcontactform/contact.php) or relative URL ( /contact.php) where the data is submitted to when user submits the form.
  • method: the method attribute specifies how the web browser sends the form data. You can use POST or GET. The POST method allows you to send large amounts of data, while the GET method is useful for sending small amounts of data via URL.

In order to read the data from the HTML form, you need to use one of the following superglobal variables:

  • $_GET array contains a list of field names and their values of the form that uses the GET method.
  • $_POST array contains a list of field names and their values of the form that uses the POST method.
  • $_REQUEST array contains field names and their values of both $_GET and $_POST, as well as values in the $_COOKIE superglobal array.

The following picture is an HTML contact form that we are going to create:

PHP Contact Form

We will use the POST method in the contact form so we need to read the form data from the $_POST array. For example, we can read the name field value from the $_POST array as follows:

However, we should always verify the data that users submit to the server in order to prevent malicious users who are trying to hack the site. PHP provides a very handy function named filter_var() that not only removes any illegal characters from the data but also checks if the data is in valid form.

The following example demonstrates how to remove any illegal characters from the name field in the form:

To check whether an email is in the proper format, we use the following code snippet:

We used the filter_var() function twice:

  • First, we removed all illegal characters from the email form field value.
  • Second, we checked if the email is in proper format.

To make the form nicer, we will use Twitter Bootstrap framework.

How the contact form works.

First, we define a list of global variables that we can use throughout the functions:

Second, in the validate_form() function, we check all the form fields. If there is any error occurred, we log the error in the $errors global array.

Third, the display_value() function displays field value if it exists in the $_POST array. We use the display_value() function to retain the field values between subsequent requests in case there is an error occurred. This helps user save time fill out the form fields again.

Fourth, the display_message() function displays error or success message based on the $errors global array.

Fifth, the start_form() function checks if a user submitted the form. If yes, it will validate the form by calling the validate_form() function. If everything is fine, it will send out an email using the mail() function.

You can download the script files in the download link below and play with the form in your system.

PHP Contact Form (1646 downloads)

In this tutorial, we’ve shown you how to work with HTML form and how to process HTML form in PHP.

  • Was this tutorial helpful ?
  • YesNo