When you fill out a form on a website and hit submit, where does that data actually go? If you've ever wondered that, you're about to get your answer. PHP gives you two main ways to grab data sent from a form: $_GET and $_POST. They do the same thing โ receive data โ but they go about it completely differently.
GET vs POST
Think of GET as writing data on a sticky note and slapping it on the URL. You can see it, anyone looking over your shoulder can see it, and it lives in the browser history. That makes GET perfect for things like search queries or filters where you might want to bookmark the result.
POST, on the other hand, sends data in the request body. You can't see it in the URL, it doesn't end up in browser history, and it can handle way more data. Use POST for things like login forms, signups, or any time you're sending sensitive or large amounts of data.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST["username"];
echo "Hello, " . $name;
}
?>
<form method="POST">
<input type="text" name="username">
<input type="submit">
</form>
Try it Yourself โ
The form method attribute
The method attribute on a form tells the browser whether to use GET or POST. If you don't specify one, it defaults to GET. That means your form data will show up in the URL as a query string โ everything after the ?. With POST, the data gets tucked away in the HTTP request body where only your server-side code can see it.
<?php
// With $_GET, data comes from the URL
// Example: page.php?search=php
if (isset($_GET["search"])) {
echo "You searched for: " . $_GET["search"];
}
?>
Try it Yourself โ
Query strings in URLs
A query string is everything after the ? in a URL. It looks like ?name=John&age=25. With GET, PHP automatically parses that into an associative array inside $_GET. The keys are the parameter names, the values are what the user typed or clicked.
The key takeaway? GET is for looking up data. POST is for changing data. Choose wisely and your web apps will make sense to everyone using them.