PHP Forms Introduction
What Are Forms?
In HTML, we can create forms with inputs like text fields, text areas, checkboxes, radio buttons, etc. to get some data from the user. Then, we can submit the user input to the server and process them.
In web development, handling a HTML form is perhaps the most important process. Two steps are involved:
- Creating the HTML form
- Creating the corresponding PHP script that will receive and process the form data
Where Are Forms Used?
Almost every website has forms. Including,
- Comment forms
- Subscribe forms
- Contact us forms
- Search Forms
Forms are really handy when creating dynamic websites.
Form HTTP Methods
You know, HTTP is a protocol, a set of rules, which makes data communication possible. This protocol defines some methods to indicate the action to be performed on the server. Each method
There are two main HTTP methods that we often use to create PHP forms.
When you open a link in your browser, the browser uses the GET method by default. In both methods, data can be sent as key-value pairs.
GET vs POST
|Data is sent in the URL.||Data is sent in the body of the HTTP request.|
|Request can be cached and saved in the browser history.||Request cannot be cached and saved in the browser history.|
|Never used to work with sensitive data (ex: email, passwords).||Used to work with sensitive data.|
|Used to get or fetch data.||Used to send data.|
|Have some length restrictions.||No length restriction. |
(But, servers will have size limitations)
Form Handling with PHP
PHP is very powerful in handling HTML forms. Its built-in functions make form validation easy and secure. In this chapter, we went through an overview of PHP forms. In the later chapters, we will learn:
- How to create a HTML form
- How to create the PHP handler
- How to validate the data
- And, more about PHP Forms...