GET and POST

Getting Started with PHP
Getting Started with PHP : Home
Introduction
Installation
Hello World
Data Types in PHP
Variables in PHP
Operators in PHP
Decision Making in PHP
Iterations in PHP
Functions in PHP
Arrays in PHP
Strings in PHP
Object Oriented PHP
Classes and Objects
Constructors
Inheritance
Object Interfaces
Static and Final
Advanced Features in PHP
GET and POST
Session in PHP
Cookies in PHP
Server Variables

Both these methods make varibales available from client browser to PHP engine and both of these are superglobal i.e. you can use both can be used in all scopes.
GET Method
Get method passes variables using the URL parameters. The variables are passed using ? at the end of the URL and then giving values of the variables. You can pass multiple variables using by using & between two variables.You might have notices this in the URL of many websites.
$_GET is an array of all the variables which were made available through GET method i.e. URL parameters. You can access any of those variables with index of $_GET['index'] as the name of the variable.

<?php
$address =$_SERVER['REQUEST_URI']."?var=2";
// This will show a button will address having var = 2
if(!isset($_GET['var'])){
echo "<a href='".$address."'>Button</a>";}

// If get array has index var this code will print
if (isset($_GET['var'])) {
    echo "<br>".$_GET['var'];
}
?>


Output

isset():
This functions checks if a variable has been given a value or not and returns boolean. We used this function to check if $_GET['var'] is set or given a value.

Click the button and then notice the address of the page. You will see the arguments passed using URL.The $_SERVER['REQUEST_URI'] variable is used to get the address of the page and remember it only gives the address after the host.
We gave button, the link of the same page just with ?var=2 at the end. We can access this var value anywhere in the code using $_GET['var'].
We can also use this with any other page as well. This method not limited to the same page only. Usage : addressofpage?var=value

POST method
POST method is used in accessing data submitted from html forms. POST method is one of the most commonly used method to get input from user.

Here we present user with a form and ask for input. Once the user submits the form the browser is redirected to the page it was instructed using action in form tag. If there is no value in action it will be redirected to the same page.

Variables of post method are accessed using $_POST['variablename'] and it is also superglobal i.e. can be used in any scope.

<form method="post" action="">
    <input type="text" name="name" id="na" placeholder="Name">
    <input type="submit" name="submit">
</form>
<?php
// If name is available in post array it will print that value
if(isset($_POST['name'])){
    echo $_POST['name'];
}
?>


Output

Remember while accessing $_POST variables the index will be the name of input not id.
Here <input type="text" name="name" id="na" placeholder="Name">, we will access this input using $_POST['name'] not $_POST['na'].

We hope you are familiar with html form. Here in form tag, there are two attributes, method and action.

$_REQUEST
It is an array that contains all the varibles from both GET and POST method. Other than these it also contains Cookies. So we could have used $_REQUEST['var'] instead of $_GET['var'] or could have replaced $_POST['name'] by $_REQUEST['name'].

Login to Track Your Progress