EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

PHP Form

Home » Software Development » Software Development Tutorials » PHP Tutorial » PHP Form

PHP Form

Introduction to PHP Form

Before understanding what is form in PHP, let’s understand what a form is?

The form is a document that contains a couple of blank fields that the user has to fill 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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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 method 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.

Popular Course in this category
PHP Training (5 Courses, 3 Project)5 Online Courses | 3 Hands-on Project | 28+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,726 ratings)
Course Price

View Course

Related Courses
Java Servlet Training (6 Courses, 12 Projects)All in One Software Development Bundle (600+ Courses, 50+ projects)

Forms are written inside form tag i.e. <form> and </form>. 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 </form> tag.

Steps to create a form are as follows:

  • We have to open and close a form inside the HTML tags using <form>………..</form> 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:

Method post

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:

PHP Form eg2

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:

forms

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.

Recommended Articles

This is a guide to PHP Form. Here we discuss the basic concept, how to create forms, methods, syntax of PHP form along with examples and code implementation. You may also look at the following articles to learn more-

  1. Introduction To PHP
  2. Sessions in PHP
  3. While Loop in PHP
  4. PHP Integer

PHP Training (5 Courses, 3 Project)

5 Online Courses

3 Hands-on Project

28+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PHP Tutorial
  • Advanced
    • Overloading in PHP
    • Overriding in PHP
    • Method Overloading in PHP
    • Inheritance in PHP
    • Multiple Inheritance in PHP
    • PHP Interface
    • Encapsulation in PHP
    • PHP Constants
    • PHP Magic Constants
    • PHP Regular Expressions
    • PHP GET Method
    • PHP Annotations
    • PHP Encryption
    • PHP file Functions
    • PHP readfile
    • PHP?Write File
    • PHP Append File
    • PHP Type Hinting
    • PHP Filters
    • PHP Float
    • PHP Form
    • PHP Form Validation
    • Sorting in PHP
    • PHP usort()
    • PHP Stack Trace
    • PHP Stack Overflow
    • PHP Pagination
    • PHP implode
    • Polymorphism in PHP
    • Abstract Class in PHP
    • PHP Final Class
    • PHP Custom Exception
    • error_reporting() in PHP
    • PHP Log Errors
    • Access Modifiers in PHP
    • PHP Change Date Format
    • Static Method in PHP
    • PHP File Handling
    • PHP Output Buffering
    • Get IP Address in PHP
    • Upload a File in PHP
    • String in PHP
    • Public Function in PHP
    • Private in PHP
    • Protected in PHP
    • basename in PHP
    • Validation in PHP
    • PHP mail()
    • PHP Email Form
    • PHP Directory
    • PHP Create Session
    • PHP include_once
    • PHP json_decode
    • PHP XMLWriter
    • PHP XML Reader
    • PHP XML Parser
    • PHP XML into Array
    • Phalcon Framework
  • PHP Basic
    • Introduction To PHP
    • What is PHP
    • PHP Keywords
    • Advantages of PHP
    • Career In PHP
    • Comments in PHP
    • PHP Commands
    • PHP Frameworks
    • PHP Compiler
    • Variables in PHP
    • PHP Superglobal Variables
    • PHP Versions
    • Object in PHP
    • What is Drupal
    • Top PHP Frameworks
    • WebStorm IDE
    • What is phpMyAdmin?
    • PhpStorm
    • Install phpMyAdmin
    • Phalcon Model
  • Data Types
    • PHP Data Types
    • PHP Integer
    • PHP Booleans
  • Operators
    • PHP Operators
    • Arithmetic Operators in PHP
    • Comparison Operators in PHP
    • Logical Operators in PHP
    • Bitwise Operators in PHP
    • Ternary Operator in PHP
    • PHP String Operators
  • Control Statements
    • Control Statement in PHP
    • PHP if Statement
    • if else Statement in PHP
    • elseif in PHP
    • PHP Switch Statement
    • Continue in PHP
    • Break in PHP
  • Loops
    • PHP Loops
    • For Loop in PHP
    • PHP Do While Loop
    • PHP While Loop
    • While Loop in PHP
    • Foreach Loop in PHP
  • Constructor
    • Constructor in PHP
    • Destructor in PHP
  • State Management
    • Cookie in PHP
    • Sessions in PHP
  • Array
    • What is PHP Array
    • Arrays in PHP
    • 2D Arrays in PHP
    • Associative Array in PHP 
    • Multidimensional Array in PHP
    • Indexed Array in PHP
    • PHP Array Functions
    • PHP unset Array
    • PHP Append Array
    • PHP Array Search
    • PHP Split Array
    • PHP array_push()
    • PHP array_pop()
  • Functions
    • Functions in PHP
    • PHP Math Functions
    • PHP Recursive Function
    • PHP String Functions
    • Hashing Function in PHP
    • Date Function in PHP
    • PHP Anonymous Function
    • Calendar in PHP
    • PHP Call Function
    • PHP Pass by Reference
    • PHP ucfirst()
    • PHP ucwords()
    • trim() in PHP
    • isset() Function in PHP
    • PHP replace
    • PHP fpm
    • PHP strpos
    • preg_match in PHP
    • PHP preg_replace()
    • PHP ob_start()
    • PHP Reflection
    • PHP Split String
    • PHP URL
    • PHP preg_match_all
    • PHP strtoupper()
    • PHP preg_split()
    • PHP substr_replace()
    • PHP setlocale()
    • PHP substr_count()
    • PHP Serialize
    • PHP strlen()
    • PHP async
    • PHP Date Time Functions
    • PHP timezone
    • PHP Data Object
    • print_r() in PHP
    • PHP header()
    • PHP strip_tags()
    • PHP chop()
    • PHP MD5()
    • PHP unset()
    • PHP crypt()
    • PHP wordwrap()
    • PHP is_null()
    • PHP strtok()
    • PHP bin2hex()
    • PHP parse_str()
    • PHP levenshtein()
    • PHP addslashes()
    • PHP strtotime
    • PHP sha1()
    • PHP explode()
    • PHP sscanf()
    • PHP require_once
    • PHP Zip Files
    • PHP $_SERVER
    • PHP $_POST
    • PHP Include and Require
    • PHP POST Method
  • Programs
    • Patterns in PHP
    • Star Patterns in PHP
    • Swapping in PHP
    • Fibonacci Series PHP
    • Factorial in PHP
    • Reverse String in PHP
    • Square Root in PHP
    • Random Number Generator in PHP
    • Palindrome in PHP
    • Prime Numbers in PHP
    • Armstrong Number in PHP
    • Socket Programming in PHP
    • Login Page in PHP
    • PHP Login Template
    • PHP Object to String
  • Database
    • PHP Database Connection
    • How to Connect Database to PHP
  • Interview Questions
    • PHP Interview Questions
    • PHP OOP Interview Questions
    • CakePHP Interview Questions
    • Core PHP Interview Questions

Related Courses

PHP Training Course

Java Servlet Training

Software Development Course Training

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - PHP Training (5 Courses, 3 Project) Learn More