Home > Backend Development > PHP Tutorial > How Can I Retrieve Input Field Values Using PHP\'s $_GET and $_POST?

How Can I Retrieve Input Field Values Using PHP\'s $_GET and $_POST?

Linda Hamilton
Release: 2024-11-24 09:06:10
Original
812 people have browsed it

How Can I Retrieve Input Field Values Using PHP's $_GET and $_POST?

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">
Copy after login

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'];
Copy after login

Form Method

The method used in your HTML form determines which superglobal is used to retrieve the input field value:

  • GET: Uses the $_GET superglobal to store form data as query string parameters.
  • POST: Uses the $_POST superglobal to store form data directly in the request body.

Example Form with GET Method:

<input type="text" name="subject">
Copy after login

To display the value using $_GET:

echo $_GET['subject']; // Output: "Car Loan"
Copy after login

Example Form with POST Method:


  <input type="text" name="subject">
Copy after login

To display the value using $_POST:

echo $_POST['subject']; // Output: "Car Loan"
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template