PHP / PHP Forms and User Input

Processing HTML Forms with PHP

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

Tutorial 1 of 5 5 resources in this section

Section overview

5 resources

Covers handling forms, sanitizing, and validating user input.

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!

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Color Palette Generator

Generate color palettes from images.

Use tool

Base64 Encoder/Decoder

Encode and decode Base64 strings.

Use tool

PDF Compressor

Reduce the size of PDF files without losing quality.

Use tool

QR Code Generator

Generate QR codes for URLs, text, or contact info.

Use tool

Timestamp Converter

Convert timestamps to human-readable dates.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help