Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

$_REQUEST

$_REQUEST is a PHP superglobal variable that holds submitted form data along with all cookie data.

In essence, it is an array that combines data from $_GET, $_POST, and $_COOKIE.

You can access this data using the $_REQUEST keyword followed by the name of the form field or cookie, like this:

$_REQUEST['firstname']

Using $_REQUEST on $_POST Requests

POST requests typically involve data submitted from an HTML form.

Here’s an example of what an HTML form might 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 the action attribute of the <form> tag.

In that PHP file, we can use the $_REQUEST variable to retrieve the values of the input fields.

PHP file

$name = $_REQUEST['fname'];
echo $name;

 

In the example below, we have combined the HTML form and PHP code within the same PHP file.

Additionally, we have included 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>
 
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
  $name = htmlspecialchars($_REQUEST['fname']);
 if (empty($name)) {
    echo "Name is empty";
 } else {
    echo $name;
 }
}
?>

</
body>
</html>