W3Schools.com (2024)

$_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>

Try it Yourself »

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>

Try it Yourself »

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

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.

W3Schools.com (2024)
Top Articles
Latest Posts
Article information

Author: Carmelo Roob

Last Updated:

Views: 5701

Rating: 4.4 / 5 (45 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Carmelo Roob

Birthday: 1995-01-09

Address: Apt. 915 481 Sipes Cliff, New Gonzalobury, CO 80176

Phone: +6773780339780

Job: Sales Executive

Hobby: Gaming, Jogging, Rugby, Video gaming, Handball, Ice skating, Web surfing

Introduction: My name is Carmelo Roob, I am a modern, handsome, delightful, comfortable, attractive, vast, good person who loves writing and wants to share my knowledge and understanding with you.