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> </td><td>
</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> 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.
$_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> </td><td>
</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> 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
PHP: Tips of the Day
How to Sort Multi-dimensional Array by Value?
Try a usort, If you are still on PHP 5.2 or earlier, you'll have to define a sorting function first:
Example:
function sortByOrder($a, $b) { return $a['order'] - $b['order']; } usort($myArray, 'sortByOrder');
Starting in PHP 5.3, you can use an anonymous function:
usort($myArray, function($a, $b) { return $a['order'] - $b['order']; });
And finally with PHP 7 you can use the spaceship operator:
usort($myArray, function($a, $b) { return $a['order'] <=> $b['order']; });
To extend this to multi-dimensional sorting, reference the second/third sorting elements if the first is zero - best explained below. You can also use this for sorting on sub-elements.
usort($myArray, function($a, $b) { $retval = $a['order'] <=> $b['order']; if ($retval == 0) { $retval = $a['suborder'] <=> $b['suborder']; if ($retval == 0) { $retval = $a['details']['subsuborder'] <=> $b['details']['subsuborder']; } } return $retval; });
If you need to retain key associations, use uasort() - see comparison of array sorting functions in the manual
Ref : https://bit.ly/3i77vCC
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework