In PHP, We can create and use forms to get data from a Form. To read or get data from a form, we need to use SuperGlobal Variables. A Superglobal variable is a built in PHP variable that is available in any scope.
PHP supports two superglobal variables, Those are
- contains list of all input element names and values sent by a form using the get method.
- contains list of all input element names and values sent by a form using the post method.
In simple words, the form request generated through may be get or post method. To retrieve data from get request, we need to use $_GET, To retrieve data from post request, we need to use $_POST.
Now, we going to learn about Get and Post forms with suitable examples.
Get form is a form which is generated through get request. The get request is the default form request. The data passed through get request is visible on the URL so it is not secured. And we can able send limited amount of data through get request.
Here, I am attaching an example to work with get request form, which consists two programs one is HTML file(GetForm.html) to create an user interface and second one is PHP file(getform.php) to read data from HTML input elements.
<!doctype html>
<html lang="en">
<title>Get Form</title>
</head>
<body><br>
<form action="getform.php" method="get">
Name:<input type="text" name="uname"></input><br/><br/>
Age:<input type="number" name="age"></input><br/><br/>
<input type="submit" value="Click Me"></input>
</form>
</body>
</html>
When the user clicks on the "Click Me" button the following PHP script file gets executed. The PHP script reads the data from HTML form elements using $_GET superglobal variable and then displays on the screen.
<!doctype html>
<html lang="en">
<title>Get Form</title>
</head>
<body><br>
<?php
// To read data from input elements using $_GET
$name = $_GET["uname"];
$age = $_GET["age"];
// To display data
echo "Welcome ".$name."<br/>";
echo "Your age is ".$age;
?>
</body>
</html>
Post form is a form which is generated through post request. The post request is not a default form request. The data passed through post request is not visible on the URL so it is secured. And it allows you send unlimited amount of data.
The Post request is widely used to submit form that have large amount of data such as file upload and sensitive data such as login form, registration form etc.
Here, I am attaching an example to work with post request form, which consists two programs one is HTML file(PostForm.html) to create an user interface and second one is PHP file(postform.php) to read data from HTML input elements.
<!doctype html>
<html lang="en">
<title>Post Form</title>
</head>
<body><br>
<form action="postform.php" method="post">
UserName :<input type="text" name="uname"></input><br/><br/>
Password :<input type="password" name="pwd"></input><br/><br/>
<input type="submit" value="Login"></input>
</form>
</body>
</html>
When the user clicks on the "Login" button the following PHP script file gets executed. The PHP script reads the data from HTML form elements using $_POST superglobal variable and then displays on the screen.
<!doctype html>
<html lang="en">
<title>Post Form</title>
</head>
<body><br>
<?php
// To read data from input elements using $_POST
$uname = $_POST["uname"];
$password = $_POST["pwd"];
// To display data
echo "Welcome ".$uname."<br/>";
echo "Your password is ".$password;
?>
</body>
</html>
<html>
<head>
<title>Radiobutton Example</title>
</head>
<body>
<form action="radio.php" method="post">
<b>1.PHP is a _____________ language?</b>
<br/><br/>
<input type="radio" name="q1" value="Programming"> Programming</input><br/>
<input type="radio" name="q1" value="Server-side Scripting"> Server-side Scripting</input><br/>
<input type="radio" name="q1" value="Client-side Scripting"> Client-side Scripting</input><br/>
<input type="radio" name="q1" value="None of the above"> None of the above</input><br/>
<br/>
<input type="submit" value="Show"/>
</form>
</body>
</html>
<html>
<head>
<title>Result</title>
</head>
<body>
<?php
$ans=$_POST['q1'];
echo "Selected answer is :$ans <br/><br/>";
echo "Correct answer is : Server-side Scripting";
?>
</body>
</html>
<html>
<head>
<title>List Example</title>
</head>
<body>
<form action="list.php" method="post">
<b>Branch : </b>
<select name="branch">
<option value="CSE">CSE</option>
<option value="ECE">ECE</option>
<option value="EEE">EEE</option>
<option value="MECH">MECH</option>
<option value="CIVIL">CIVIL</option>
</select>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
<html>
<head>
<title>List</title>
</head>
<body>
<?php
$branch=$_POST['branch'];
echo "Your Branch is : <b>$branch </b><br/><br/>";
?>
</body>
</html>