PHP › Form Handling & Validation
How do you retrieve data submitted through an HTML form using PHP, and what is the difference between the GET and POST methods?
BeginnerAnswer
Form data submitted using the GET method is appended directly to the URL as a query string and accessible through the $_GET superglobal, which is appropriate for non sensitive requests like a search query, while form data submitted using the POST method is sent within the request body rather than the URL and accessed through the $_POST superglobal, which is the appropriate choice for submitting sensitive data or data that modifies something on the server, such as a login form or a purchase.
<form method="post" action="submit.php">
<input type="text" name="username">
</form>
$username = $_POST['username'] ?? '';Real-world example A search feature uses the GET method so users can bookmark or share a specific search results URL, while a password change form uses the POST method to avoid exposing the submitted password directly within the URL.