Open In App

What is the use of the $_REQUEST variable in PHP ?

Uses of PHP $_REQUEST:

Approach: The example demonstrates an HTML form with the input field and within having to submit button. When a user submits the information by clicking on the button “Submit”, the filled data is sent to the file i.e. examined with actions attributes of the <form> tag within it



We aim the file itself for preprocessing form data as we use another PHP file to preprocess a form data, replacing that with occurring filename of our choice. We use the PHP $_REQUEST super global variable to collect the value of the input field from it.

Example:






<!DOCTYPE html>
<html>
    
<body>
   <!--Defining Form-->
   <h1 style="color:green">GeeksforGeeks</h1>
   <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
      Name: <input type="text" name="pname">
      <input type="submit">
   </form>    
      
    <?php
  
        // Getting $REQUEST METHOD
        if ($_SERVER["REQUEST_METHOD"]=="POST") {
            $name = htmlspecialchars($_REQUEST['pname']); 
  
            // Collecting the value of input field from it
            if (empty($name)) {
                echo "Name is empty";
            
            else {
               // Printing the entered data
                echo $name;    
           }
        }
    ?>
</body>
</html>

Output:

For The String “GeeksforGeeks”


Article Tags :