Tuesday, 31 January 2017

$_REQUEST AND QUERY STRING IN PHP

$_REQUEST – By using this super global variable, we can get posted values of get and post method. We can also get values of cookie and Query string.


Query String – It is a small amount of data on URL address followed by ? symbol. Along with form data, if we want to transfer some extra information from one page to another page at the time of redirection we can use query string. Query string can be passed in two ways –

1. Query string as name and value.
2. Query string with only value.

Note – Using $_REQUEST, we can get value of query string if it is a combination of name and value.


Example –

form.html
<a href="p1.php?empno=1001&empname=Larry">Click</a>

p1.php
<?php
echo $_REQUEST['empno']."<br>";
echo $_REQUEST['empname'];
?>

Output –
1001
Larry



Example –

form.html
<form method="get" action="p1.php">
Text1 <input type="text" name="t1"><br>
Text2 <input type="text" name="t2"><br>
<input type="submit" name="Sub" value="Click">
</form>

p1.php
<?php
echo $_REQUEST['t1']."<br>";
echo $_REQUEST['t2'];
?>


Example –

form.html
<form method="post" action="p1.php">
Text1 <input type="text" name="t1"><br>
Text2 <input type="text" name="t2"><br>
<input type="submit" name="Sub" value="Click">
</form>

p1.php
<?php
echo $_REQUEST['t1']."<br>";
echo $_REQUEST['t2'];
?>


Example –

form.html
<a href="p1.php?iname=tr.jpg">
<img src="tr.jpg" width="100px"></a>
<a href="p1.php?iname=saradnja.png">
<img src="saradnja.png" width="100px"></a>

p1.php
<?php
echo $_REQUEST['iname']."<br>";
$inm=$_REQUEST['iname'];
echo "<img src='$inm' width='100px'>";

?>

No comments:

Post a Comment