$_REQUEST
$_REQUEST
is a PHP super global variable which contains submitted form data, and all cookie data.
In other words, $_REQUEST
is an array containing data from$_GET
,$_POST
, and$_COOKIE
.
You can access this data with the $_REQUEST
keyword followed by the name of the form field, or cookie, like this:
$_REQUEST['firstname']
Using $_REQUEST on $_POST Requests
POST request are usually data submitted from an HTML form.
Here is an example of how a HTML form could look like:
HTML form
<html><body><form method="post" action="demo_request.php"> Name: <input type="text" name="fname"> <input type="submit"></form></body></html>
When a user clicks the submit button, the form data is sent to a PHP file specified in theaction
attribute of the<form>
tag.
In the action file we can use the $_REQUEST
variableto collect the value of the input field.
PHP file
$name = $_REQUEST['fname'];echo $name;
In the example below we have put the HTML form and PHP code in the same PHP file.
We have also added some extra lines for security.
Example
<html><body><form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>"> Name: <input type="text" name="fname"> <input type="submit"></form><?phpif ($_SERVER["REQUEST_METHOD"] == "POST") { $name = htmlspecialchars($_REQUEST['fname']); if (empty($name)) { echo "Name is empty"; } else { echo $name; }}?></body></html>
Using $_REQUEST on $_GET Requests
GET request can be form submissions as in the example above, with themethod
attribute of the HTML <form>
element set to GET
.
GET requests can also be data from a query string (information added after a URL address).
Here is an example of how an HTML hyperlink, with a query string could look like:
HTML link
<html><body><a href="demo_phpfile.php?subject=PHP&web=W3schools.com">Test $GET</a></body></html>
When a user clicks the link, the query string data is sent to demo_phpfile.php
.
In the PHP file we can use the $_REQUEST
variableto collect the value of the query string.
Example
The PHP file demo_phpfile.php
:
<html><body><?phpecho "Study " . $_REQUEST['subject'] . " at " . $_REQUEST['web'];?></body></html>
W3schools Pathfinder
Track your progress - it's free!
×
Contact Sales
If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com
Report Error
If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com
Top Tutorials
HTML Tutorial
CSS Tutorial
JavaScript Tutorial
How To Tutorial
SQL Tutorial
Python Tutorial
W3.CSS Tutorial
Bootstrap Tutorial
PHP Tutorial
Java Tutorial
C++ Tutorial
jQuery Tutorial
Top References
HTML Reference
CSS Reference
JavaScript Reference
SQL Reference
Python Reference
W3.CSS Reference
Bootstrap Reference
PHP Reference
HTML Colors
Java Reference
Angular Reference
jQuery Reference
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.
Copyright 1999-2024 by Refsnes Data. All Rights Reserved.W3Schools is Powered by W3.CSS.