Php-mysql Course

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:
https://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:
https://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:
https://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 type of <input> creates a date input control, such as a pop-up calendar?
type="text" type="date" type="button"
<input type="date" name="set_date" value="2012-10-15" />
Which CSS property adds shadow effects to the text of an element?
font-style color text-shadow
h2 {
  text-shadow: 2px 3px 3px #a0a1fe;
}
Click on the function that adds new elements to the end of an array.
pop() shift() push()
var pags = ["lessons", "courses"];
pags.push("download", "tutorials");
alert(pags[2]);            // download
Which function sorts an array by key, in ascending order, maintaining key to data correlations?
asort() ksort() sort()
$lang =[10=>"PHP", 20=>"JavaScript", "site"=>"coursesweb.net");
ksort($lang);
var_export($lang);     // array ("site"=>"coursesweb.net", 10=>"PHP", 20=>"JavaScript")
$_GET, $_POST and $_REQUEST Variables

Last accessed pages

  1. Node.js Course (2844)
  2. Animated Tooltip with HTML and CSS (285)
  3. PHP-MySQL free course, online tutorials PHP MySQL code (69549)
  4. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (142959)
  5. Working with MySQL Database (3260)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (192)
  2. PHP-MySQL free course, online tutorials PHP MySQL code (19)
  3. The Mastery of Love (19)
  4. CSS cursor property - Custom Cursors (18)
  5. Read Excel file data in PHP - PhpExcelReader (15)