How to use REQUEST method in PHP to get form values?


This is a 'superglobal', or automatic global variable. $_REQUEST is an associative array consisting of the contents of $_GET, $_POST, and $_COOKIE ie, it is available in all scopes throughout a script.
Example: Create a simple form which contains variables handled by GET,POST method and COOKIE variable. A Simple Form:


When the user fill the form and click submit button the form will be submitted to request.php.
Coding to create a simple form
setcookie("user", "Alex");
foreach($_REQUEST as $val)
<form action="request.php?age='25'&city='cbe'" method="POST">
Name: <input type="text" name="name">
Email: <input type="text" name="mail">
<input type="submit" value="Submit">
In the above code form filed variable such as "name" and "mail" is handled by POST method as we mentioned method=POST. Variables such as "age" and "city" are passed through url so it will be accessed only by GET method and here cookie is set for variable "user " with value "Alex" by using setcookie function. Save above code with the name getvariable.php.
When the user enter the values for name and email and clicks the submit button, variables with the values are submitted to request.php. Here variables handled by different methods are printed. Thus $_REQUEST variable is an associative array used to get the values of POST,GET and COOKIE variables.

PHP Topics

Ask Questions

Ask Question