PHP Get and Post Methods to Retrieve Form Data

PHP Get and PHP Post methods are used in Form handling when we want to retrieve Form data on submit.

What is PHP Form Handling?

PHP form handling is the method of retrieving user-entered form data using get and post methods. You can use $_POST and $_GET superglobals when you use to post and get methods on form submission.

When the user enters all its data in various form control fields like input box, radio, select boxes etc and clicks on the submit button, the entered data supplied to the file you put in the action attribute of the form tag.

The actioned file contains the PHP code with superglobals variables to retrieve and manipulate form data.

PHP Get Method

The PHP Get Method can be used to send the form data and get the data visible in the URL. However, the method is not secured as anyone can see the form data.

The values of form data can be seen in the URL separated by an ampersand(&) as given below. You can send a limited amount of data by using the get method for form handling.

Never use PHP Get method when you have a username, password, and other sensitive data to send to the server.

Anyone can get your password and username with this method. The passed data is fully visible to the visitors and it gets saved in their browser’s history. The user can get the data anytime they want and you may lose your security with it.

Example: How to receive form data from PHP get method
For this, you need two files, the first file is a form file that contains the form and its controls. The second file contains the PHP codes which you can use to get the form submitted data and use them to collect your visitor’s input information.

Let’s see this for example files given below.

Form.html

getFormdata.php
You can use $_GET superglobals to retrieve form data in php coding.

Output

Visitor name is Bill and email is [email protected]

The output URL after user submission with PHP get method will be as given below.

PHP Post Method

The PHP Post Method can be used to send the secure form data and the data is not visible in the URL. This method is the widely used method by the developers to send a large amount of data included username and password.

As this method sends the data without displaying it in the URL. You can use this method for your login form, contact form, and many sensitive form type.

The user entered information is secure, you can use this method to retrieve a large amount of data like file uploading.

This PHP post method also provides a superglobal variable $_POST to retrieve the form data when the user clicks the submit button with this method.

Example: How to receive form data from PHP post method
For this method, you need two files, the first file is a form file(e.g.Form.html)that contains form controls like input box and others. Here, we use only input boxes. The second file is the action file that contains PHP codes to retrieve the user submitted data and use them to collect your visitor’s input information.

Let’s see this for example files given below.

Form.html

postFormdata.php
You can use $_POST superglobal variable to retrieve form data using php coding.

Output

Visitor name is Bill and email is [email protected]

The output URL after user submission with PHP post method will be as given below.

You can check one thing here, the output is same for both PHP post and PHP get method. The only difference is the URL in which get method displays all user-entered information while post method does not display this.

You must read:-

Reference