GET Method

Introduction

The GET method is used to send data from a web form to the server through the URL. It is one of the most commonly used HTTP request methods in PHP and web development.

When a user submits a form using the GET method, the form data becomes visible in the browser’s address bar as query parameters.

Objectives

By the end of this training, you will be able to:

  • Understand the GET method in PHP
  • Create forms using the GET method
  • Retrieve user input using $_GET
  • Pass data through URLs
  • Understand the advantages and limitations of GET requests

What is the GET Method

The GET method sends form data through the URL.

Example URL:

example.com/page.php?name=Ali&city=Lahore

In this example:

  • name=Ali
  • city=Lahore

These are query parameters sent to the server.

Syntax of GET Method

HTML Form Example

<form method="GET" action="welcome.php">
<input type="text" name="username">
<input type="submit" value="Submit">
</form>

Accessing GET Data in PHP

<?php
$username = $_GET['username'];

echo "Welcome " . $username;
?>

How GET Method Works

  1. The user fills out the form
  2. The browser sends data through the URL
  3. PHP receives the data using the $_GET superglobal
  4. The server processes and displays the result

Example of GET Method

HTML Form

<form method="GET">
Name: <input type="text" name="name">
<input type="submit">
</form>

PHP Code

<?php
if(isset($_GET['name'])) {
$name = $_GET['name'];
echo "Hello " . $name;
}
?>

Sending Multiple Values

<form method="GET">
First Name: <input type="text" name="fname">
Last Name: <input type="text" name="lname">
<input type="submit">
</form>
<?php
$first = $_GET['fname'];
$last = $_GET['lname'];

echo $first . " " . $last;
?>

Advantages of GET Method

Easy to Use

GET requests are simple and beginner friendly.

Bookmarking

URLs with GET parameters can be bookmarked and shared.

Fast Data Transfer

Suitable for small amounts of data.

Useful for Search Pages

Often used in search forms and filters.

Limitations of GET Method

Data Visible in URL

Information can be seen in the browser address bar.

Limited Data Length

URLs have character limits.

Less Secure

Sensitive information should not be sent using GET.

Difference Between GET and POST

FeatureGETPOST
Data LocationURLRequest Body
VisibilityVisibleHidden
SecurityLess SecureMore Secure
Data SizeLimitedLarge Amount
Bookmark SupportYesNo

Best Practices

  • Use GET for search and filter forms
  • Avoid sending passwords using GET
  • Validate all user inputs
  • Use isset() before accessing GET variables

Real World Uses of GET Method

  • Search forms
  • Product filters
  • Pagination
  • URL parameters
  • Website navigation

Final Presentation

In your final presentation, explain:

  • What the GET method is
  • How GET works in PHP
  • How to create GET forms
  • How to access data using $_GET
  • Advantages and disadvantages of GET
  • Difference between GET and POST methods
Home » Intermediate PHP > Forms Handling > GET Method