PHP TUTORIALPHP TutorialPHP InstallationPHP Hello WorldPHP Basic SyntaxPHP CommentsPHP VariablesPHP Variable ScopePHP ConstantsPHP StringsPHP OutputPHP Data TypesPHP Type CastingPHP OperatorsPHP ConditionalsPHP Shorthand ConditionalsPHP LoopsPHP Loop Control StructuresPHP FunctionsPHP String FunctionsPHP ArraysPHP Superglobal VariablesPHP in HTMLPHP AdvancedPHP Include and RequirePHP RegexRegex IntroductionRegex PCRE SyntaxPHP PREG FunctionsPHP FormsPHP Forms IntroductionPHP Forms CreatingPHP Forms SecurityPHP Forms ValidationPHP Forms Required InputsPHP Forms StickyPHP Forms Advanced ValidationPHP Forms FinishingPHP OOPPHP OOP IntroductionPHP OOP ClassesPHP OOP PropertiesPHP OOP ObjectsPHP OOP MethodsPHP OOP $this KeywordPHP OOP Constructors and DestructorsPHP OOP VisibilityPHP OOP InheritancePHP OOP Abstract Classes and MethodsPHP OOP InterfacesPHP OOP TraitsPHP OOP ConstantsPHP OOP StaticPHP OOP Namespaces

PHP Forms Introduction

Forms are used to collect data from the users

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
Note: In this tutorial, we will focus on handling the forms in PHP. And, we will slightly go through the process of creating the HTML form. If you haven't learned HTML forms earlier, we would recommend you to go through a tutorial.

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.

  • GET
  • POST

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

GETPOST
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...
Facebook Twitter