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.
Section overview
5 resourcesCovers 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
- Create a form with two fields: "Username" and "Password". Process this form using the POST method in PHP.
- 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.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Random Password Generator
Create secure, complex passwords with custom length and character options.
Use toolLatest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI 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 articleAI 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 articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article