GET or POST method helps to submit and transfer the data in the forms. Both are similar, but post method is very secure for the data submission in the form as it transfers the data in hidden format from page to page.
Using get method, the data is carried out through the
URL
which is visible and is insecure when compared with post method.
Data Transfer through GET Method
The data transferred through GET Method will be stored in the superglobal variable
$_GET
.This data submitted can be accessed in the form using GET Method by
$_GET
global variable.
[php]
<html>
<body>
<form action="#" method="get">
Name : <input type="text" name="name" />
Email : <input type="text" name="email" />
Contact : <input type="text" name="contact" />
<input type="submit" name="submit" value="Submit"></input>
</form>
<?php
if( $_GET["name"] || $_GET["email"] || $_GET["contact"])
{
echo "Welcome: ". $_GET['name']. "<br />";
echo "Your Email is: ". $_GET["email"]. "<br />";
echo "Your Mobile No. is: ". $_GET["contact"];
}
?>
</body>
</html>
[/php]
Output:
Data Transfer through POST Method
The data transfer through POST Method will be stored in the superglobal variable
$_POST
.This data submitted in the form can be accessed using POST Method by
$_POST
global variable.
[php]
<html>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>" name="User-details">
Name : <input type="text" name="name" />
Mobile : <input type="tel" name="phone" />
<input type="submit" name="submit" value="Send" />
</form>
<?php
if ($_POST['submit'] == "Send")
{
$name = $_POST['name'];
$mobile = $_POST['mobile'];
if (empty($name)&& empty($mobile))
{
echo "Name & Mobile Number is empty";
}
else
{
echo $name;
echo $mobile;
}
}
?>
</body>
</html>
[/php]
Output: