Retrieving Input Field Value Using PHP
When working with HTML forms, it often becomes necessary to access and manipulate user input from input fields. PHP provides several mechanisms for doing this, including the $_POST and $_GET superglobals.
Using $_POST or $_GET
To retrieve the value of an input field using PHP, you can use either the $_POST or $_GET superglobals. These superglobals contain an associative array of form field values, keyed by the name of the HTML input tag.
Example
Consider the following input field:
<input type="text" name="subject">
To get the value of this input field and assign it to a session, you can use the following PHP code:
// Using $_GET $_SESSION['subject'] = $_GET['subject']; // Using $_POST $_SESSION['subject'] = $_POST['subject'];
Form Method
The method used in your HTML form determines which superglobal is used to retrieve the input field value:
Example Form with GET Method:
To display the value using $_GET:
echo $_GET['subject']; // Output: "Car Loan"
Example Form with POST Method:
To display the value using $_POST:
echo $_POST['subject']; // Output: "Car Loan"
The above is the detailed content of How Can I Retrieve Input Field Values Using PHP\'s $_GET and $_POST?. For more information, please follow other related articles on the PHP Chinese website!