This content originally appeared on CodeSource.io and was authored 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.
The post How to Get Input Value Using $_REQUEST in PHP appeared first on CodeSource.io.
This content originally appeared on CodeSource.io and was authored by Ariessa Norramli
Ariessa Norramli | Sciencx (2021-03-06T05:36:17+00:00) How to Get Input Value Using $_REQUEST in PHP. Retrieved from https://www.scien.cx/2021/03/06/how-to-get-input-value-using-_request-in-php/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.