Preserving User Input in PHP
PHP $_REQUEST
PHP $_REQUEST is used to collect data after submitting an HTML form. The example below shows a form with an input field and a submit button.
When a user submits the data by clicking on "Submit", the form data is sent to the file specified in the action attribute of the tag. In this example, we point to this file itself for processing form data.
If you wish to use another PHP file to process form data, replace that with the filename of your choice. Then, we can use the super global variable $_REQUEST to collect the value of the input field:
Example:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// collect value of input field
$name = $_REQUEST['fname'];
if (empty($name)) {
echo "Name is empty";
} else {
echo $name;
}
}
?>
<html>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Name: <input type="text" name="fname">
<input type="submit">
</form>
</body>
</html>PHP $_POST
PHP $_POST is widely used to collect form data after submitting an HTML form with method="post".
$_POST is also widely used to pass variables. The example below shows a form with an input field and a submit button.
When a user submits the data by clicking on "Submit", the form data is sent to the file specified in the action attribute of the tag.
In this example, we point to the file itself for processing form data. If you wish to use another PHP file to process form data, replace that with the filename of your choice.
Then, we can use the super global variable $_POST to collect the value of the input field:
Example:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// collect value of input field
$name = $_POST['fname'];
if (empty($name)) {
echo "Name is empty";
} else {
echo $name;
}
}
?>
<html>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Name: <input type="text" name="fname">
<input type="submit">
</form>
</body>
</html>PHP $_GET
PHP $_GET can also be used to collect form data after submitting an HTML form with method="get". $_GET can also collect data sent in the URL.
Assume we have an HTML page that contains a hyperlink with parameters:
<html>
<body>
<a href="test_get.php?subject1=PHP&subject2=MySQL">Test $GET</a>
</body>
</html>When a user clicks on the link "Test $GET", the parameters "subject1" and "subject2" are sent to "test_get.php", and can then access their values in "test_get.php" with $_GET.
The example below shows the code in “test_get.php”.
Example:
<html>
<body>
<?php
echo "Study " . $_GET['subject1'] . " and " . $_GET['subject2'];
?>
</body>
</html>