Week 3. HTML Forms
Chapter 9
Introducing PHP Forms
1. What Are Forms?
- Forms are an essential part of web development used to collect input from users and send that data to the server for processing.
- They enable user interaction with web applications, such as submitting data, searching, or logging in.
2. Why Use Forms in PHP?
- Dynamic Input Handling: Forms allow developers to handle dynamic user inputs, such as names, email addresses, or search queries.
- Interactivity: They provide a bridge between the user interface and backend processing.
- Data Storage and Processing: User inputs can be stored in databases or processed immediately, enabling tailored responses.
3. PHP and Form Processing
- PHP provides superglobal variables like
$_GET
and$_POST
to easily retrieve and process data submitted by forms. - It supports data validation and sanitization, ensuring secure and reliable form submissions.
4. Best Practices for PHP Forms
- Always validate and sanitize user inputs to prevent vulnerabilities like SQL injection or XSS.
- Use methods like
POST
for sensitive data submissions (e.g., passwords). - Provide clear feedback or error messages for user-friendly interactions.
Note: Properly designed forms enhance user experience and ensure secure interactions with your web application.
Assignments
- $_GET Form
- $_POST Form
- Simple "Sticky" Form
- All In One Form
- Your creative assignment is to demonstrate your command of PHP to provide these controls to the user and how to gather information from the user.
Demos
The above shows how to set up a form to gather information from a user via GET and POST super globals.
The "Simple Sticky Form" demonstrates how to pass the values a user provides to re-populate the form. Note: This example passes the values via the POST super global. It works for the GET super global in same fashion.
The "All in One Form" shows many of the HTML controls that you may use to get a user's input to a set of values. For example, instead of asking for a "true" or "false" from a user, you can have them click a checkbox. Also, instead of asking a use to pick "one of something", you can ask via a set of radio buttons. These controls show you how to gather users input.
The Simple Dropdown Form shows how to use one of the controls shown in the "All in One Form", namely the Dropdown menu.
The Forms - "Form - NOT Secure" and "Form - Secure" are examples that show how JavaScript code can be blocked. You do not want a bad actor to use your form to launch a JavaScript routine (like a mass emailing of spam).
The "Redirect Form" shows how a simple radio button can redirect a Browser to another Website or code.