"I am Saqib Jahangir. A passionate vlogger, software engineer, trainer and avid traveler with a deep love for exploring the hidden gems of our beautiful planet. With a strong foundation in Application Development, Application Architecture & Database Design and Product Management, I bring over a decade of hands-on experience building secure, scalable, and resilient web applications for a diverse range of industries."

Forms and PHP (GET and POST)

 

Forms in PHP are used to collect user input from the browser and send it to the server for processing. The two most common methods for sending form data are GET and POST.


GET Method

·         Sends form data through the URL.

·         Data is visible in the browser's address bar.

·         Best for search queries or when bookmarking is needed.

·         Limited amount of data can be sent.

Example (GET Form):

<!-- get_form.html -->
<form action="get_process.php" method="get">
    Name: <input type="text" name="username">
    <input type="submit" value="Submit">
</form>
 
<?php
// get_process.php
$name = $_GET['username'];
echo "Hello, " . htmlspecialchars($name);
?>

If the user enters John, the URL will look like:


POST Method

·         Sends form data in the HTTP request body (not visible in the URL).

·         More secure than GET for sensitive information.

·         Can send larger amounts of data.

·         Commonly used for registration and login forms.

Example (POST Form):

<!-- post_form.html -->
<form action="post_process.php" method="post">
    Email: <input type="email" name="email">
    <input type="submit" value="Submit">
</form>
<?php
// post_process.php
$email = $_POST['email'];
echo "Your email is: " . htmlspecialchars($email);
?>

Key Differences Between GET and POST


GET : URL

Data Size Limit: 

Limited (~2000 characters)

Security:
Less secure (data visible)

POST: HTTP Request Body

Data Size Limit: 

Much larger

Security:
More secure (data hidden)

Security Note

Always sanitize user input using functions like htmlspecialchars() or validation methods to prevent XSS and SQL Injection attacks.

 

Popular Posts

Operators (Arithmetic, Comparison, Logical)

Functions (Built-in & User-defined)