How to Get Input Value Using $_REQUEST in PHP

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…

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

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


Print Share Comment Cite Upload Translate Updates
APA

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/

MLA
" » How to Get Input Value Using $_REQUEST in PHP." Ariessa Norramli | Sciencx - Saturday March 6, 2021, https://www.scien.cx/2021/03/06/how-to-get-input-value-using-_request-in-php/
HARVARD
Ariessa Norramli | Sciencx Saturday March 6, 2021 » How to Get Input Value Using $_REQUEST in PHP., viewed ,<https://www.scien.cx/2021/03/06/how-to-get-input-value-using-_request-in-php/>
VANCOUVER
Ariessa Norramli | Sciencx - » How to Get Input Value Using $_REQUEST in PHP. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/03/06/how-to-get-input-value-using-_request-in-php/
CHICAGO
" » How to Get Input Value Using $_REQUEST in PHP." Ariessa Norramli | Sciencx - Accessed . https://www.scien.cx/2021/03/06/how-to-get-input-value-using-_request-in-php/
IEEE
" » How to Get Input Value Using $_REQUEST in PHP." Ariessa Norramli | Sciencx [Online]. Available: https://www.scien.cx/2021/03/06/how-to-get-input-value-using-_request-in-php/. [Accessed: ]
rf:citation
» How to Get Input Value Using $_REQUEST in PHP | Ariessa Norramli | Sciencx | 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.

You must be logged in to translate posts. Please log in or register.