Friday, 23 February 2018

GET and POST methods in PHP

$_GET and $_POST is used to get data or information from forms.
$_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 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;  
}
?>
Output:

 

 

Result:


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

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;  
}
?>
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

apply function in R

1) apply function: It takes 3 arguments matrix,margin and function.. Example: m<-matrix(c(1,2,3,4),nrow=2,ncol=2) m #1 indicates it is ap...