Sending form data using GET and POST methods

Sending form data using GET and POST methods

In PHP programming, HTML user-filled form data information can be sent or uploaded to a dedicated web server location by applying the GET or POST method. These two methods in HTML forms allow web developers to determine whether to store or send form data to a dedicated host server for processing and storage in the client’s web browser. Here, web developers can apply the GET or POST method to HTML forms as needed.

Sending form data using GET and POST methods

So, let’s learn more about the GET or POST method in PHP programming using HTML.

GET Method in PHP with HTML

The GET method in HTML forms sends form data to a dedicated web server location by defining a URL. The form GET method is defined by adding data in a query string key-value pair format to the URL specified in the action attribute of the HTML form.

Key features of the GET method in HTML.

  • The Form Get method appends data information from an HTML form to a URL in query parameter format.
  • The Get method previews form data in the web browser’s URL bar, indicating that it is not secure.
  • The length of the form URL in the Get method is limited; it varies depending on the web browser used, typically 2000 characters or less.
  • The Get method is not suitable for confidential user data such as passwords.

The Get method in HTML forms is typically used to retrieve form data or when the form data does not modify the server state, such as a webpage or a search query.

Example of sending form data using GET in HTML.

<!DOCTYPE html>

<html lang=”en”>

<head>

<meta charset=”UTF-8″>

<meta name=”viewport” content=”width=device-width, initial-scale=1.0″>

<title>GET Form Method Example</title>

</head>

<body>

<h1>Get Form Method</h1>

<form action=”search_results.php” method=”GET”>

<label for=”query”>Find Query</label>

<input type=”text” id=”query” name=”query” required><br><br>

<input type=”submit” value=”Find Query”>

</form>

</body>

</html>

How to send form data through Get Form method.

Here, in this form, when the user presses the submit button, the existing form data will be sent to a URL in a query string format like this:

http://example.com/search_results.php?query=some+search+term

HTML web developers can get query parameters using PHP programming on the server-side with the $_GET form method.

Example PHP GET method in HTML search_results.php.

<?php

if (isset($_GET[‘query’])) {

$search_query = $_GET[‘query’];

echo “You may search here: ” . htmlspecialchars($search_query);

}

?>

POST method in HTML forms.

The POST method in HTML forms sends user-filled form data to the website webpage in the body portion of the HTTP protocol request, instead of appending it to the URL. This process makes the POST method in HTML forms more suitable for sending large amounts of data information or sensitive confidential data.

Key features of a form POST in HTML.

  • The POST method sends the data request in the form body portion, so it is not previewed in the URL, making it a more secure method for confidential and sensitive form data information.
  • There is no limit on the size of form data sent with the POST form method; ultimately, this is controlled by the dedicated server host configuration.
  • The POST method in HTML webpages is commonly used for forms that modify form data information on a dedicated web server. For example, user login forms, online registration forms, or submitting large amounts of data.

Example of sending form data using the POST method in an HTML form.

<!DOCTYPE html>

<html lang=”en”>

<head>

<meta charset=”UTF-8″>

<meta name=”viewport” content=”width=device-width, initial-scale=1.0″>

<title>Form POST Method Example</title>

</head>

<body>

<h1>User Login Form</h1>

<form action=”login_process.php” method=”POST”>

<label for=”Full name”>Full name </label>

<input type=”text” id=”fullname” name=”fullname” required><br><br>

<label for=”department”>Department</label>

<input type=”text” id=”department” name=”department” required><br><br>

<label for=”password”>Password</label>

<input type=”password” id=”password” name=”password” required><br><br>

<input type=”submit” value=”Login Access”>

</form>

</body>

</html>

How to send data using the POST method in an HTML form.

When an Internet user submits data and information to an HTML form, the form data is not previewed in the webpage URL. It is sent to the form in portion format within the HTTP request body.

For example, the request to the web server will be previewed like this; here the form data is not previewed in the URL.

POST /login_process.php

Body:

username=amit&password=testpassword

Web developers can receive form data on a dedicated server-side by implementing the $_POST method in PHP programming.

Example of the POST method in login_process.php.

<?php

if (isset($_POST[‘fullname’]) && isset($_POST[‘password’])) {

$fullname = $_POST[‘fullname’];

$department = $_POST[‘department’];

$password = $_POST[‘password’];

// Here it processes login to check entered user credentials

echo “logic succeeded, ” . htmlspecialchars($fullname) . “.”;

}

?>

When to use the GET and POST method in an HTML form?

Use the GET method in an HTML form when.

  • In an HTML form, when users are receiving data that does not modify the current server state. For example, a search or filtering query.
  • Use the GET method in an HTML form when the data in the URL may be previewed, and this preview does not include confidential sensitive user data information, such as query previews.
  • Use the GET method in an HTML form when the volume of data received is small, such as query parameters.

Use the POST method in an HTML form when.

  • In an HTML form, when web visitors are posting or submitting data that will modify the current server state. For example, user login, form registration, file upload, etc.
  • Use the POST method in an HTML form when the data contains sensitive information, such as secure passwords, personal details, etc. The POST HTML form method is used when a web visitor needs to send a large amount of data, such as forms with many form fields, custom user file uploads, etc.

Example of both GET and POST methods in a single HTML form.

Sometimes, an HTML web developer may apply both GET and POST form methods to a single form for group purposes.

An example of a form with GET and POST in an HTML form.

<!DOCTYPE html>

<html lang=”en”>

<head>

<meta charset=”UTF-8″>

<meta name=”viewport” content=”width=device-width, initial-scale=1.0″>

<title>Html Form GET and POST Method Example</title>

</head>

<body>

<h1>Find and Submit Form Example</h1>

<!– here we use a GET method for search query in html form –>

<form action=”search.php” method=”GET”>

<label for=”find_query”>Find</label>

<input type=”text” id=”find_query” name=”find_query”>

<br><br>

<input type=”submit” value=”Find Query”>

</form> <br>

<!– here we use POST method for form data submission –>

< form action=”submit.php” method=”POST”>

<label for=”fullname”>Full Name </label>

<input type=”text” id=”fullname” name=”fullname”><br><br>

<label for=”email”>Email </label>

<input type=”email” id=”email” name=”email”><br><br>

<input type=”submit” value=”Submit Query”>

</form>

</body>

</html>

Form with GET and POST method in HTML explanation.

Here, the GET form method is applied to the first search query, i.e., it adds the user search query to the URL.

The second POST method form uses the POST method to submit user data information online, i.e., it sends the form data request in the body portion.

Main difference between html form GET vs POST method

Form MethodGET form methodPOST form method
Visibility of Data infoGet method display form Data visible in the receive website URL.Post method form secure Data is not visible in the send website URL.
Data send receive LengthGet form method Limited to send usually 2000 characters.Post form method has No practical limit for send large volume data.
Data SecurityPost form method is Not secure method for data can be intercepted during sending.Post form method is More secure when secure confidential data is sent in the body portion.
Usage caseGet form method is Suitable for retrieving data e.g, search query related data.Post form method is Suitable for sending sensitive confidential or large volume data e.g, user login, form submissions etc.
Caching BehaviorGet method Can be cached by web browsers and GET requests can be bookmarked.In post form method Not cached by web browser because POST method data is not saved.
IdempotencyGET method requests should not have side effects it’s a safe for use method.POST form requests are intended to have side effects e.g. because its saving secure user data.

Conclusion of the GET and POST form methods in an HTML form.

The GET method in an HTML form is most useful for receiving user data online, and when the received form data is not secure or large-volume. It appends the form data to a URL, and the form file is not previewed by the user.

The POST method in an HTML form is used to send large-volume or confidential form file data in a secure format, and the POST method is most useful when the form data is modifying the host server state, such as submitting a user form or login credentials.

Leave a Reply