$_GET and $_POST is used to get data or
information from forms.
Example:
Result:
$_GET and $_POST are super global variables.
The form
request may be get or post. To retrieve data from get request, we need to use
$_GET, for post request $_POST.
Get method
Get method
Get
request is the default form request.
The data passed through get request is
visible on the URL browser so it is not secured. We can
send limited amount of data through get request.
Example:
demo.html
<html>
<body>
<form method="get" action="add.php">
Enter First Number:
<input type="number" name="num1" /><br>
Enter Second Number:
<input type="number" name="num2" /><br>
<input type="submit" name="submit" value="Add">
</form>
</body>
</html>
add.php
<?php
if(isset($_GET['submit']))
{
$number1 = $_GET['num1'];
$number2 = $_GET['num2'];
$sum = $number1+$number2;
echo "The sum of $number1 and $number2 is: ".$sum;
}
?>
<html>
<body>
<form method="get" action="add.php">
Enter First Number:
<input type="number" name="num1" /><br>
Enter Second Number:
<input type="number" name="num2" /><br>
<input type="submit" name="submit" value="Add">
</form>
</body>
</html>
add.php
<?php
if(isset($_GET['submit']))
{
$number1 = $_GET['num1'];
$number2 = $_GET['num2'];
$sum = $number1+$number2;
echo "The sum of $number1 and $number2 is: ".$sum;
}
?>
Output:
Note:
The data passed through get request is visible on the URL browser .Here we passed get request so information is visible in the browser
Post method
Post method
You have to write post method if you want to make your page secure
The data passed through post request is not
visible on the URL browser so it is secured. You can send large amount of data
through post request.
Post request is widely used to submit form
that have large amount of data such as file upload, image upload, login form,
registration form etc.
Example:
demo.html
<html>
<body>
<form method="post" action="add.php">
Enter First Number:
<input type="number" name="num1" /><br>
Enter Second Number:
<input type="number" name="num2" /><br>
<input type="submit" name="submit" value="Add">
</form>
</body>
</html>
add.php
<?php
if(isset($_POST['submit']))
{
$number1 = $_POST['num1'];
$number2 = $_POST['num2'];
$sum = $number1+$number2;
echo "The sum of $number1 and $number2 is: ".$sum;
}
?>
<body>
<form method="post" action="add.php">
Enter First Number:
<input type="number" name="num1" /><br>
Enter Second Number:
<input type="number" name="num2" /><br>
<input type="submit" name="submit" value="Add">
</form>
</body>
</html>
add.php
<?php
if(isset($_POST['submit']))
{
$number1 = $_POST['num1'];
$number2 = $_POST['num2'];
$sum = $number1+$number2;
echo "The sum of $number1 and $number2 is: ".$sum;
}
?>
Output:
Result:
Note:
The data passed through post request is not visible on the URL browser .Here we passed post request so information is not visible in the browser
No comments:
Post a Comment