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
What attribute makes a radio button or checkbox input selected?
checked="checked" selected="selected" disabled="disabled"
<input type="checkbox" name="a_name" value="value" checked="checked" />
What CSS value scales the background image to the largest size contained within the element?
repeat-x contain linear-gradient
#id {
  background:url("path_to_image.png");
  background-size:contain;
  background-repeat:no-repeat;
}
What operator is used to determine the rest of the division of two numbers?
% * /
var rest8_7 = 8 % 7;
alert(rest8_7);
Indicate the PHP function that rounds a number up to the next highest integer.
floor() ceil() abs()
$nr = ceil(3.5);
echo $nr;        // 4
$_GET, $_POST and $_REQUEST Variables

Last accessed pages

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (142070)
  2. Date and Time in ActionScript 3 (10078)
  3. Voting Poll System script PHP-AJAX (8743)
  4. ActionScript 3 Lessons (7369)
  5. Merge Multiple Files, Line by Line (1137)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (73)
  2. The Mastery of Love (17)
  3. PHP-MySQL free course, online tutorials PHP MySQL code (10)
  4. CSS cursor property - Custom Cursors (10)
  5. PHP Unzipper - Extract Zip, Rar Archives (9)