The method attribute of a form can have two values: GET and POST. The difference between GET and POST methods lies in how the information is transmitted to the PHP script.

PHP $_GET

The $_GET variable is a superglobal Array that contains data from a form sent with method="get" or from URL.
Information sent from a form with the GET method will be displayed in the browser's address bar (so, is visible to everyone) and has limits on the amount of data to send (about 2,048 characters).
  - Example:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<body>

<form action="script.php" method="get">
 User: <input type="text" name="user" /><br />
 <input type="radio" name="gen" value="man" />Man
 <input type="radio" name="gen" value="woman" />Woman<br />
 <input type="submit" value="Send" />
</form>

</body>
</html>
After the user fills the form and clicks the "submit" (Send) button, the URL sent to the server could look something like this:
http://coursesweb.net/script.php?user=MarPlo&gen=man
The "script.php" file can use the $_GET variable /array to collect form data (the names of the form fields are the keys in the $_GET array).
<?php
if (isset($_GET['user']) && isset($_GET['gen'])) {
  $user = $_GET['user'];
  $gen = $_GET['gen'];

  echo 'User: '. $user. ' - gender: '. $gen;
}
?>

Before using data received through get (or post), is indicated to check if they were set /sent (using the "isset()" function).


If you want to use in a PHP script data sent directly from a link, that data must be added in the URL address after the "?" character, in key=value pairs separated by "&" character.
  - Example:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<body>

<a href="index.php?src=php&id=8" title="PHP">PHP Tutorial</a>

</body>
</html>
This link example has an URL with two keys in the query string (after the "?" character), one called src and the other called id. In the "index.php" file you can manipulate these values. All you have to do is reference the key within the $_GET.
<?php
if (isset($_GET['src']) && isset($_GET['id'])) {
  // php instructions
}
else {
  // other php instructions
}
?>

PHP $_POST

The $_POST variable is a superglobal Array that contains data from a form sent with method="post".
Information sent from a form with the POST method is invisible to the user and hasn't a specified limit on the amount of information to send. By default, PHP permits up to 8MB of POST data (can be changed from the post_max_size in the php.ini file).
  - Example:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<body>

<form action="script.php" method="post">
 Name: <input type="text" name="user" />
 <input type="submit" value="Send" />
</form>

</body>
</html>
When the user clicks the "submit" (Send) button, script.php is called, the URL will look like this:
http://coursesweb.net/script.php
The "script.php" file can use the $_POST variable /array to collect form data (the names of the form fields are the keys in the $_POST array).
<?php
if (isset($_POST['user'])) {
  echo 'Welcome '. $_POST['user'];
}
else {
 echo 'Welcome visitor';
}
?>

PHP $_REQUEST

The PHP $_REQUEST variable is a superglobal Array that contains the contents of both $_GET, $_POST, and $_COOKIE arrays. It can be used to collect data sent with both the GET and POST methods.
  - Example:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<body>

<form action="script.php?id=789" method="post">
 Name: <input type="text" name="user" />
 <input type="submit" value="Send" />
</form>

</body>
</html>
This code submits a form via the POST method, and when the form is posted, the code sends some values along the URL via the GET method ( "id=789", defined in the address from "action" attribute). The URL will look like this:
http://coursesweb.net/script.php?id=789
The sent data are loaded in the superglobal $_REQUEST variable /array, and can be used to collect both GET and POST data. For example:
<?php
if (isset($_REQUEST['id']) && isset($_REQUEST['user'])) {
  $id = $_REQUEST['id'];
  $user = $_REQUEST['user'];

  echo 'Welcome '. $user. ' - ID: '. $id;
}
else {
 echo 'Welcome visitor';
}
?>
• If there are data received via GET and POST at the same time (through URL address and from a form), with the same "name" to both ($_GET['name'] and $_POST['name'), the $_REQUEST['name'] will contain the value of $_POST['name'].
- Example:
<?php
if(isset($_REQUEST['thename'])) echo $_REQUEST['thename'];       // CoursesWeb.net
?>
<form action="zz.php?thename=MarPlo" method="post">
 <input type="text" name="thename" value="CoursesWeb.net" />
 <input type="submit" value="Send" />
</form>

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag renders as emphasized text, displaying the text oblique?
<strong> <pre> <em>
<p>Web development courses: <em>CoursesWeb.net</em></p>
Which CSS property defines the space between the element border and its content?
margin padding position
h3 {
  padding: 2px 0.2em;
}
Click on the method which returns the first element that matches a specified group of selectors.
getElementsByName() querySelector() querySelectorAll()
// gets first Div with class="cls", and shows its content
var elm = document.querySelector("div.cls");
alert(elm.innerHTML);
Indicate the PHP variable that contains data from a form sent with method="post".
$_SESSION $_GET $_POST
if(isset($_POST["field"])) {
  echo $_POST["field"];
}
$_GET, $_POST and $_REQUEST Variables

Last accessed pages

  1. Bubbles3 (43093)
  2. Mushroom Revolution (552)
  3. Get Attribute (ID, Class, Name, Title, Src) with jQuery (37816)
  4. Zoo Volley (160)
  5. Sport Games (7005)

Popular pages this month

  1. Qwop (6303)
  2. Drag Racer V3 (4297)
  3. Bubbles3 (1797)
  4. List with All the Games (1481)
  5. Butterfly Kyodai (1413)