PHP $_GET and $_POST

PHP offers two associative arrays $_GET and $_POST corresponding to HTTP GET and POST methods. These are used to carry the values from the page on which form action and method is defined to the .php script called. The form elements pass their values to either of these arrays as per defined method for current HTML form.

$_GET

This is the associative array corresponding to GET method under HTTP requests. This associative array gets its values from the variables defined in the current web page or PHP script through the HTTP GET method. When a user presses the submit button on a form with GET method the encoded data is attached to the URL after a “?” symbol. The browser sends the request to local or remote server depending upon the location of the requested script.

$_GET is a super global variable. It means that it is accessible across the web pages and scripts of a web application. It must not be declared since it is inbuilt in PHP and gets the values after an HTTP GET request from client.

Example

Form.HTML

 <form action="hello.php" method="get">
User Name: <input type="text" name="uname" /><BR><BR>
Date Of Birth: <input type="text" name="udob" /><BR><BR>
Password: <input type="password" name="pwd" /><BR><BR>
<input type="submit" value="Press to Register"/>
</form>

$_GET FORM

 Hello New User<?php echo ' '.$_GET["uname"]; ?>.<br>
You were born on <?php echo ' '. $_GET["udob"].' '; ?> and you are 
<?php
$date1=date_create("25-09-2018");
$date2=date_create($_GET["udob"]);
$diff= date_diff($date1,$date2); 
echo $diff->format('%y Year %m Month %d Day ')?> old!<BR><BR>
<input type="submit" value="press to continue to site"/>

 

When the form is submitted with a GET method it reads the form element values and  variables . These are appended as name=value pairs after URL and “?”  as visible in the address bar of the following image. A special character is replaced by % and two digit octal equivalent of the symbol in the query string. Here for in value for control “Date of Birth” the symbol  “/” is sent as %2F. Each name=value pair is separated by &.

$_GET Script

$_POST

This is another associative array corresponding to POST method under HTTP request. This associative array also gets its values from the variables defined in the current web page orPHP script through the HTTP POST method. When a user presses the submit button on a form the browser sends the request to local or remote server depending upon the location of the requested script. The important feature of using POST method is that encoded information collected from the form or scripts is never attached to the URL of the requested page.

Like $_GET, $_POST is also super global. It must also not be declared since it is inbuilt in PHP and gets the values after an HTTP POST request made by client.

 <form action="hello.php" method="POST">
User Name: <input type="text" name="uname" /><BR><BR>
Date Of Birth: <input type="text" name="udob" /><BR><BR>
Password: <input type="password" name="pwd" /><BR><BR>
<input type="submit" value="Press to Register"/>
</form>

$_POST FORM

 Hello New User<?php echo ' '.$_POST["uname"]; ?>.<br>
You were born on <?php echo ' '. $_POST["udob"].' '; ?> and you are 
<?php
$date1=date_create("25-09-2018");
$date2=date_create($_POST["udob"]);
$diff= date_diff($date1,$date2); 
echo $diff->format('%y Year %m Month %d Day ')?>
old!<BR><BR>
<input type="submit" value="press to continue to site"/>

$_POST Script

The $_GET and $_POST arrays can be used to make your web applications respond to the input entered by a user.