HOME HTML EDITOR C JAVA PHP

PHP Form Handling

When an HTML form is submitted, all input values are automatically sent to PHP's predefined Superglobal variables. PHP uses these variables to read and process the user's data on the server.

1. How is Data Received?

PHP uses two primary variables to capture form data, depending on the method used in the HTML form:

2. The POST Method Example

The POST method sends data within the HTTP request body. Because the data is not visible in the URL, it is the preferred method for login forms and sensitive information.

<!-- index.php -->
<form action="welcome.php" method="post">
  Name: <input type="text" name="name">
  Email: <input type="text" name="email">
  <input type="submit">
</form>

In welcome.php, you can display the submitted data like this:

<?php
  echo "Welcome " . $_POST["name"];
  echo "Your email address is: " . $_POST["email"];
?>

3. The GET Method Example

The GET method appends the form data to the URL. This is commonly used for search results, bookmarks, or filtering data.

<?php
  // URL will look like: welcome.php?name=Rahul&email=rahul@example.com
  echo "Hi " . $_GET["name"];
?>

4. GET vs. POST: Which one to use?

Important Note: Developers always prefer the POST method when saving data to a database, as it is much more secure and versatile than GET.