PHP Form
Before understanding what is form in PHP, let’s understand what form is?
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
The form is a document that contains a couple of blank fields that the user has to fill in the data or the user can select the data. The user’s data is stored in the database with the respective user’s data and can be retrieved anytime and anywhere needed.
PHP Form
Form in PHP is similar to the forms that are built using HTML except the syntax used. In PHP, forms use the GET and POST methods to print or retrieve the data entered by the user.
When the user enters all the details required in the form and submits the form using the submit button the form is then further sent for processing and the action is performed on the basis of whatever is mentioned in the action function. The form is then sent for further processing using GET or POST methods whichever is mentioned while designing the form.
How to Create a Form in PHP and its Syntax?
Forms are used to get the inputs from the user and process the data into the database or submits the data to the corresponding web server for processing purposes. The form contains the HTML tags which will be having the GUI (Graphical User Interface) such as radio buttons, checkboxes, etc.
These components are used in the form so that the user must feel easy to interact with the GUI/webpage or fill the content of the form. Forms are specially prepared for user-friendly purposes where the user who doesn’t have technical knowledge will explore the form in different ways to use it.
Forms are written inside form tag i.e.
. These tags define that the code for form has started and all the input boxes, checkboxes, radio buttons, etc. can be included inside the form and the form can be closed using tag.Steps to create a form are as follows:
- We have to open and close a form inside the HTML tags using tags.
- After the form is written it has to be submitted using either GET or POST methods.
- If you have to include various attributes like input boxes, checkboxes, radio buttons, etc.
- The submission of the form will process the data filled by the user and the necessary actions will be done.
Syntax
<html> <head> <title> Sample Form Page </title> </head> <body> <h1> Form Sample </h1> <form action="<sample.php>" method="<GET/POST>" > // Two methods GET or POST method to be chosen Name: <input type = "text" name="<name that has to be given>” <input type="submit" value="<what you want to show to user for e.g. submit button"> </form> </body> </html>
In the above program, the syntax has been written for the form elements for the user to fill the details for registration purposes of a name. The user will fill the data in the input box mentioned in the program and the user will click on the submit button to process the data and the action of the form will be performed. In the form action, the PHP file mentioned will have the code to process the data in whatever method mentioned in the form i.e. either GET or POST.
Get and Post Methods
The given methods in PHP Form are explained below:
Get Method in PHP
In PHP, a superglobal array is used to get the values that are submitted using the HTML page via get method. It is built-in and has a global scope i.e. anyone can see the data or any script can read the data from the program. This method is used to print the data in the URL that is submitted by the user in the form. It is mainly used in the programs where the data has to be visibly entered by the user for e.g. search engines, websites, bookmarks, etc.
Post Method in PHP
In PHP, a superglobal array built-in method is used to get the values that are submitted using the HTML page via the POST method. It has a global scope i.e. anyone can see the data or any script can read the data from the program. This method is used when the user doesn’t want to display the content entered by him in the form elements. The best example of using this method is when the user is using login details for a particular website/application.
Examples of Methods in PHP Form
Here are some examples of Get and Post methods which is given below:
Example #1
Code:
<!DOCTYPE HTML> <html> <body> <form action="abc.php" method="POST"> Name: <input type="text" name="Name"><br> E-mail: <input type="text" name="Email"><br> Contact Number: <input type="text" name="Number"><br> <input type="submit" value="Submit"> </form> </body> </html>
Output:
Example #2
Code:
<!DOCTYPE HTML> <html> <head> </head> <body> <?php $Name = $Email = $Gender = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { $name = test_input($_POST["Name"]); $email = test_input($_POST["Email"]); $gender = test_input($_POST["Gender"]); } function test_input($data) { $data = trim($data); $data = htmlspecialchars($data); return $data; } ?> <h3>FORM IN PHP EXAMPLE</h3> <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> Name: <input type="text" name="Name"> <br><br> E-mail: <input type="text" name="Email"> <br><br> Gender: <input type="radio" name="Gender" value="female">Female <input type="radio" name="Gender" value="male">Male <br><br> <input type="submit" name="submit" value="Submit"> </form> </body> </html>
Output:
Example #3
Code:
<!DOCTYPE HTML> <html> <head> <style> .error {color: #FF0000;} </style> </head> <body> <?php $NameError = $EmailError = $GenderError = ""; $Name = $Email = $Gender = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { if (empty($_POST["Name"])) { $NameError = "Name is required"; } else { $Name = test_input($_POST["Name"]); } if (empty($_POST["Email"])) { $EmailError = "Email is required"; } else { $Email = test_input($_POST["Email"]); } if (empty($_POST["Gender"])) { $GenderError = "Gender is required"; } else { $Gender = test_input($_POST["Gender"]); } } function test_input($data) { $data = trim($data); $data = htmlspecialchars($data); return $data; } ?> <h3>FORM EXAMPLE IN PHP</h3> <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> Name: <input type="text" name="Name"> <span class="error">* <?php echo $NameError;?></span> <br><br> E-mail: <input type="text" name="Email"> <span class="error">* <?php echo $EmailError;?></span> <br><br> Gender: <input type="radio" name="Gender" value="female">Female <input type="radio" name="Gender" value="male">Male <span class="error">* <?php echo $GenderError;?></span> <br><br> <input type="submit" name="submit" value="Submit"> </form> </body> </html>
Output:
Conclusion
In this article, we have learned different components of form and the methods to submit the form. The developer generally uses the GET method so that the user can see what content has been entered whereas in the POST method the case is different where the user details are not displayed on the screen.
The above is the detailed content of PHP Form. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
