Processing HTML Forms with PHP

Tutorial 1 of 5

Introduction

In this tutorial, we aim to learn how to process HTML forms using PHP. We'll explore how to collect data from forms using both POST and GET methods and how to handle this data in PHP.

By the end of this tutorial, you will be able to:

  • Understand how HTML forms work
  • Use PHP to process form data
  • Understand the difference between the GET and POST methods
  • Validate form inputs

Prerequisites: Basic understanding of HTML and PHP.

Step-by-Step Guide

HTML Forms

HTML forms are a way to collect user input. The <form> element is used to create an HTML form.

HTML Form example:

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

PHP Form Handling

When a user fills out a form and clicks the submit button, the form data is sent for processing to a PHP file specified in the action attribute of the <form> tag.

The form data can be sent as HTTP POST or GET methods. These are superglobals, which means that they are always accessible, regardless of the scope, and you can access them from any function, class, or file without having to do anything special.

  • The GET method appends the form data into the URL in name/value pairs: URL?name=value&name=value
  • The POST method sends the form data in the body of the HTTP request, not in the URL, providing a more secure way of transferring sensitive data.

Code Examples

Example 1: Basic Form with POST Method

Here's a simple form where users can enter their name and email. We've set the method to POST because we don't want sensitive data like email addresses visible in the URL.

HTML Form (form.html):

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

PHP Form Processing (welcome.php):

Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>

In the PHP script, the $_POST superglobal array is used to collect the form data.

Example 2: Form with GET Method

HTML Form (form.html):

<form action="welcome_get.php" method="get">
  Name: <input type="text" name="name"><br>
  E-mail: <input type="text" name="email"><br>
  <input type="submit">
</form>

PHP Form Processing (welcome_get.php):

Welcome <?php echo $_GET["name"]; ?><br>
Your email address is: <?php echo $_GET["email"]; ?>

In this example, we use the $_GET superglobal array. The information sent from the form with the GET method is visible to everyone and displayed in the URL.

Summary

In this tutorial, we've learned how to process HTML forms using PHP. We covered the basics of HTML forms, the difference between the GET and POST methods, and how to use PHP to handle the form data.

Practice Exercises

  1. Create a form with two fields: "Username" and "Password". Process this form using the POST method in PHP.
  2. Create a form with three fields: "Firstname", "Lastname", and "Email". Display this information using the GET method in PHP. Remember, the GET method should only be used for non-sensitive data.

Additional Resources

Remember, the best way to learn is by doing. Practice by building as many forms as you can and processing them using PHP. Happy learning!