PHP GET and POST


Methods for Sending Data to the Server

The two HTTP (Hypertext Transfer Protocol) methods GET and POST are commonly used by web browsers to communicate with the server. Both methods transmit data in different ways and have different benefits and drawbacks, as explained below.


GET method

The data is transmitted as URL parameters in the GET process, generally namestrings and value pairs separated by sands (&).


http://www.example.com/action.php?name=john&age=24


By concatenating with ampersands (&), more than one parameter=value may be inserted in the URL. The GET method can only submit simple text data.

$_GET is a superglobal variable in PHP that allows you to access all data sent via URL or via an HTML form with the method="get" parameter.


<!DOCTYPE html>
<html lang="en">
<head>
<title>HP GET method examples</title>
</head>
<body>
<?php
  if(isset($_GET["name"])){
  echo "<p>Hi, " . $_GET["name"] . "</p>";
  }
?>
<form method="get" action="<?php echo $_SERVER["PHP_SELF"];?>">
<label for="inputName">Name:</label>
<input type="text" name="name" id="inputName">
<input type="submit" value="Submit">
</form>
</body>



POST method

The data is transmitted to the server via POST in a separate communication with the script. The data sent using the POST method will not appear in the URL.

PHP provides a superglobal variable $_POST to access all information sent through the post method or sent via an HTML form with the method="post" attribute, similar to $_GET.


<!DOCTYPE html>
<html lang="en">
<head>
<title>PHP POST method examples</title>
</head>
<body>
<?php
  if(isset($_POST["name"])){
  echo "<p>Hi, " . $_POST["name"] . "</p>";
  }
?>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
<label for="inputName">Name:</label>
<input type="text" name="name" id="inputName">
<input type="submit" value="Submit">
</form>
</body>



$_REQUEST Variable

The $_GET and $_POST variables, as well as the values of the $_COOKIE superglobal variable, are all stored in the $_REQUEST superglobal variable in PHP.


<!DOCTYPE html>
<html lang="en">
<head>
<title>PHP $_REQUEST variable examples</title>
</head>
<body>
<?php
  if(isset($_REQUEST["name"])){
  echo "<p>Hi, " . $_REQUEST["name"] . "</p>";
  }
?>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
<label for="inputName">Name:</label>
<input type="text" name="name" id="inputName">
<input type="submit" value="Submit">
</form>
</body>