How to Get Input Value Using $_REQUEST in PHP
Posted on: March 06, 2021 by Ariessa Norramli
In this article, you will learn how to get an input value using $_REQUEST in PHP.
Let’s say you have a simple form with an input field and a submit button.
php
<!DOCTYPE html>
<html>
<body>
<!-- A form with input field and submit button -->
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Age: <input type="text" name="fage">
<input type="submit">
</form>
</body>
</html>
Get Input Value Using $_REQUEST
In order to get an input value, you can use the htmlspecialchars()
method and $_REQUEST
variable.
full php
<!DOCTYPE html>
<html>
<body>
<!-- A form with input field and submit button -->
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Age: <input type="text" name="fage">
<input type="submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Get input field value
$age = htmlspecialchars($_REQUEST['fage']);
// If input field is empty
if (empty($age)) {
// Display message "Age is empty"
echo "Age is empty";
// Else if input field is not empty
} else {
// Display age
echo $age;
}
}
?>
</body>
</html>
Before Submitting Input
After Submitting Input
Note: The htmlspecialchars()
method functions by converting special characters to HTML entities. The $_REQUEST
variable is a built-in PHP variable that functions by getting data from the input field.
Share on social media
//