Quick Reach
Introduction to $_REQUEST
The PHP $_REQUEST variable is used to retrieve the information sent by POST, GET or Cookie. This information can be assigned or saved to the database or can be used for any intended purposes.
See an example of $_REQUEST with post
The post and get methods are used in forms to send data to the server. We have used PHP $_GET and $_POST as well to receive the form data for respected form method.
However, the $_REQUEST contains forms data sent by either of the methods as well as cookies.
Example of using PHP $_Request
The following example uses the Post method of the form to send data to the server. So information entered will not be visible in the address bar.
Experience this example online
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
<?php
if( $_REQUEST["yourname"] || $_REQUEST["yourage"] )
{
echo "Welcome ". $_REQUEST['yourname']. "<br />";
echo "You age is ". $_REQUEST['yourage']. " years.";
exit();
}
?>
<html>
<body>
<form action="<?php $_PHP_SELF ?>" method="post">
Your Name: <input type="text" name="yourname" />
Your Age: <input type="text" name="yourage" />
<input type="submit" />
</form>
</body>
</html>
|
In above example, simply replace method=”get” and you will see the same output except in address bar. See example below.
The $_request example with get method
The following example is the same as above except we used the get method of form rather post to send data to the server. As such, the request is used for both get and post methods, the retrieving method remains the same.
Experience this example online
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
<?php
if( $_REQUEST["yourname"] || $_REQUEST["yourage"] )
{
echo "Welcome ". $_REQUEST['yourname']. "<br />";
echo "You age is ". $_REQUEST['yourage']. " years.";
exit();
}
?>
<html>
<body>
<form action="<?php $_PHP_SELF ?>" method="get">
Your Name: <input type="text" name="yourname" />
Your Age: <input type="text" name="yourage" />
<input type="submit" />
</form>
</body>
</html>
|
As you test this example, you will see the same output. However, when using the get method the entered information can be seen in the address bar of the browser.
For more on which method should be used in forms depends on the scenario you are working for.
Related: PHP Get and Post
Leave A Comment?