w3resource

PHP: $_REQUEST, $_POST, $_GET

PHP: $_REQUEST

Description

$_REQUEST is a super global variable which is widely used to collect data after submitting html forms.

Here is an example:

<html>
<head>
<title>Php contact form</title>
</head>
<body>
<form name="contact" method="post" action="contact.php">
<table bgcolor=cornsilk style="border-collapse: collapse" bordercolor="#111111" cellpadding="0" cellspacing="0" align="center" >
<tr><td>&nbsp;</td><td>
&nbsp;</td></tr>
<tr><td style="font-family: verdana; font-size: 10pt" align="right">
<b><font color=#CC0000>*</font><font color=#004080> Name:</font></b></td><td>
<font color="#006600"><b><input size=25 name="name"></b></font></td></tr>
<tr><td style="font-family: verdana; font-size: 10pt" align="right"><b><font color=#CC0000>*</font><font color=#004080> Email:</font></b></td><td>
<font color="#006600"><b><input size=25 name="Email"></b></font></td></tr>
<tr><td style="font-family: verdana; font-size: 10pt" align="right"><b><font color="#004080">Address:</font></b></td><td align="right"> <font color="#006600"><b><input size=25 name="Company" style="float: left"></b></font></td></tr> <tr><td style="font-family: verdana; font-size: 10pt" align="right"> <b><font color="#004080">Contact No:</font></b></td><td> <font color="#006600"><b><input size=25 name="Phone"></b></font></td></tr> <tr><td colspan=2 style="font-family: verdana; font-size: 10pt"> <font color="#004080"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Message</b></font><b><font color="#006600">:</font></b></td></tr> <tr><td colspan=2 align=center><textarea name="Message" rows=5 cols=35></textarea></td></tr> <tr><td colspan=2 align=center><input class="formbutton" type=submit name="send" value="Submit"></td></tr> <tr><td colspan=2 align=center style="font-family: verdana; font-size: 10pt"><small>A <font color=red>*</font> indicates a field is required</small></td></tr> </table> </form> </body> </html>

Now in contact.php we can collect the data entered by the user in different fields using $_RQUEST. Suppose we want to see what data have been entered by the user in the name field, then code to do that will be:

<?php
$name=$_REQUEST['name'];
echo $name;
?>

Here is the output of the contact form:
In the contact.html file above, we have used POST as a method to send data from the form. But php allows us to use $_GET and $_COOKIE also.

PHP: $_POST

Description

$_POST is a super global variable which is widely used to pass variables. This super global variable is widely used to handle form data.

We will see an example where a particular php script is executed if after the form is submitted to another php script.

Here is an example:

<html>
<head>
<title>Php contact form</title>
</head>
<body>
<form name="contact" method="post" action="next.php">
<table bgcolor=cornsilk style="border-collapse: collapse" bordercolor="#111111" cellpadding="0" cellspacing="0" align="center" >
<tr><td>&nbsp;</td><td>
&nbsp;</td></tr>
<tr><td style="font-family: verdana; font-size: 10pt" align="right">
<b><font color=#CC0000>*</font><font color=#004080> Name:</font></b></td><td>
<font color="#006600"><b><input size=25 name="Name"></b></font></td></tr>
<tr><td style="font-family: verdana; font-size: 10pt" align="right"><b><font color=#CC0000>*</font><font color=#004080> Email:</font></b></td><td>
<font color="#006600"><b><input size=25 name="Email"></b></font></td></tr>
<tr><td style="font-family: verdana; font-size: 10pt" align="right"><b><font color="#004080">Address:</font></b></td><td align="right">
<font color="#006600"><b><input size=25 name="Company" style="float: left"></b></font></td></tr>
<tr><td style="font-family: verdana; font-size: 10pt" align="right">
<b><font color="#004080">Contact No:</font></b></td><td>
<font color="#006600"><b><input size=25 name="Phone"></b></font></td></tr>
<tr><td colspan=2 style="font-family: verdana; font-size: 10pt">
<font color="#004080"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Message</b></font><b><font color="#006600">:</font></b></td></tr>
<tr><td colspan=2 align=center><textarea name="Message" rows=5 cols=35></textarea></td></tr>
<tr><td colspan=2 align=center><input class="formbutton" type=submit name="send" value="Submit"></td></tr>
<tr><td colspan=2 align=center style="font-family: verdana; font-size: 10pt"><small>A <font color=red>*</font> indicates a field is required</small></td></tr>
</table>
</form>
</body>
</html>

Now in next.php it is checked first whether the form is submitted by checking if whether the submit button is pressed to generate a value which is the name of the button.

If the submit button is pressed, then another php script which prints a message is included in the next.php file. So. if the form contact-post.html is submitted, you should be able to see the message written to be displayed in msg.php file.

Code of msg.php file:.

<?php
echo "YOU have submitted the form"; 
?>
<?php
if ($_POST['send'])
include ('msg.php');
?>

PHP: $_GET

Description

$_GET is a super global variable which can be used to do the same job done by POST. But besides, $_GET can do some other wonderful jobs as far as passing data is concerned.

We will see an example where a some data is being sent through a link. And then those data are collected.

Here is an example:

<html>
<head>
<title>Php contact form</title>
</head>
<body>
<a href="get-pass.php?r=w3resourse.com&s=online-tutorial">This is to send data</a>  
</body> </html>

Code of get-pass.php file:

<?php 
echo $_GET['r']."is an ".$_GET['s'];
?>

Previous: $_SERVER
Next: $_FILES, $_ENV, $_COOKIE, $_SESSION



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/php/super-variables/$_REQUEST.php