php
  1. php-get-post-methods

PHP Get & Post Methods

Syntax

$_GET['parameter_name']
$_POST['parameter_name']

Example

Get Method

<form action="get_example.php" method="get">
  <label for="name">Name:</label>
  <input type="text" id="name" name="name"><br><br>
  <label for="age">Age:</label>
  <input type="text" id="age" name="age"><br><br>
  <input type="submit" value="Submit">
</form>

Post Method

<form action="post_example.php" method="post">
  <label for="name">Name:</label>
  <input type="text" id="name" name="name"><br><br>
  <label for="age">Age:</label>
  <input type="text" id="age" name="age"><br><br>
  <input type="submit" value="Submit">
</form>

Explanation

The $_GET and $_POST methods are used for retrieving data sent from an HTML form. $_GET retrieves the data by using the URL parameters while $_POST retrieves the data from form fields.

Use

The $_GET method is used when you want to retrieve data without modifying it, such as searches or sorting. The $_POST method is used when you want to modify or add data, such as creating a new user or updating an existing user.

Important Points

  • The URL will contain the form data when using $_GET.
  • The form data will not be visible in the URL when using $_POST.
  • Always sanitize the data before using it in your code to prevent SQL injections and other security issues.

Summary

In PHP, the $_GET and $_POST methods are used for retrieving data sent from an HTML form. The $_GET method retrieves data using the URL parameters while the $_POST method retrieves the data from form fields. It is important to always sanitize the data before using it in your code.

Published on: