Request Array in PHP
Before learning this array you might want to learn the HTTP GET and POST methods in the HTML section of this website.
The form fields or any parameter passed to a PHP page with POST method can be retrieved in the PHP page using the inbuilt array $_REQUEST[]. You can retrieve the array values using the indices (Positional values of parameters) or by using the names of the parameters. Using the names to retrieve the values is recommended.
The following is the syntax of a get array.
$user = $_REQUEST["userid"];
An example has been given in the next section which displays a webform with two webforms. The first one submitted using the GET method while second one, using the POST method. The form is submitted to the same page. The script displays the parameters submitted using the POST method as a response. A form is submitted using the POST method when the "method" attribute of the form is set to POST.
Save this file as request_array.php. If you do not understand the cookie, leave it. You will read about the PHP cookies in the coming sections. Here the point is related to the request method. That the request array can be used to access the cookies also.
<?
if(!isset($_COOKIE["request_cookie"]))
setcookie("request_cookie","tutorialindia.com");?>
<html>
<head>
<title>$_REQUEST Array</title>
</head>
<body>
<?php
echo "The cookie is: ".
$_REQUEST["request_cookie"];
echo "<br>User Id is: ".$_REQUEST["user"];
echo "<br>Password is: ".$_REQUEST["password"];
?>
<form action=request_array.php method=GET>
Using the GET Method: UserId:
<input type=text name=user>
Password:
<input type=text name=password>
<input type=submit value=Submit>
</form>
<form action=request_array.php method=POST>
Using the GET Method: UserId:
<input type=text name=user>
Password:
<input type=text name=password>
<input type=submit value=Submit>
</form>
</body>
</html>
- 459 reads
