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 tag can be used to create input text field in web page?
<form> <input> <div>
<input type="text" name="a_name" value="val" />
Which CSS property displays the text in a small-caps font?
display font-variant font-style
h3 {
  font-variant: small-caps;
}
What instruction displays a notice box with a message inside it, in JavaScript?
for() Date() alert()
var msg = "Visit CoursesWeb.net";
alert(msg);
Indicate the PHP code used to get the users IP.
$_SERVER["HTTP_USER_AGENT"] $_SERVER["REMOTE_ADDR"] $_GET[]
$ip = $_SERVER["REMOTE_ADDR"];
echo $ip;
$_GET, $_POST and $_REQUEST Variables

Last accessed pages

  1. Using server-sent events - EventSource (1310)
  2. SHA256 Encrypt hash in JavaScript (28516)
  3. Area and Perimeter Calculator for 2D shapes (9806)
  4. Add Text in Canvas from Input text field, as it is Typed (9488)
  5. Add Pause in JavaScript script (14836)

Popular pages this month

  1. PHP Unzipper - Extract Zip, Rar Archives (804)
  2. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (565)
  3. SHA1 Encrypt data in JavaScript (430)
  4. Create simple Website with PHP (397)
  5. Read Excel file data in PHP - PhpExcelReader (389)