Hit enter after type your search item

PHP Get: Explained with 3 examples

The $_GET in PHP

The $_GET is the way to get the values of variables that are passed to the current PHP page via URL parameters.

The PHP GET is an associative array, so variable becomes keys.

To understand that, consider an HTML form submits information by using the method = get:


 

As you submit the form to a targeted PHP file, this is how you may get that by using the $_GET:


 

The information sent through the get method is visible in the URL. For example, if you enter an email abc@gmail.com in the above form and submit the form, this is how URL should look at the localhost:


 

If form has multiple controls, then multiple variables are separated by ‘&’ as shown below:


 

(considering the target file name is test.php).

Not only you may use the $_GET PHP array with HTML forms but any other ways where URL contains parameters like sending data through jQuery $.get method or data sent through links etc.

In the following section, I will show you how you can receive data by using the PHP’s $_GET with HTML form, links and jQuery’s ajax $.get method.

An example of accessing information by $_GET with HTML form

In this example, an HTML form is created with a few fields like name, email, password, address etc. After entering the information, as you press the submit button, the form’s data will be sent to the server by using form’s get method. The target file is the same where this form is created.

The form’s data is accessed by using the $_GET array and finally it will be displayed by using the PHP’s echo statement.

See the example online by clicking the link or image below:

PHP GET

See online demo and code

In the markup section, this is how the HTML form tag is used:

 


 

So, the data is sent to the same file where a form is created and the method is “get”.

This is how $_GET array is used in the script:


 

In the address bar of the demo page, you can see all the parameters are visible and separated by “&” sign.

An example of $_GET with sending data via an HTML link

In this example, an HTML link is used to send data to the same PHP file where it is created. The link contains three variables just like used in above example.

As you click the link, the data will be passed to the PHP file via URL. The data is accessed by using the $_GET array as follows:

PHP GET HTML LINK

See online demo and code

This is how the link is created in markup section of the demo page:


 

The following PHP script is used to access URL parameters:


 

A demo of using jQuery $.get method to send data; collected in $_GET array

In this demo, the jQuery get method is used to send data to a PHP file. This method is used to make AJAX call, so after filling the form fields, as you press the button the data will be displayed without refreshing the web page, where a PHP file with $_GET is executed underneath.

PHP GET jquery

See online demo and code

In the form tag, you can see only a CSS class is used along with Bootstrap’s CSS class since this form is created based at Bootstrap framework:

<form class=”form-horizontal” role=”form”>

Even, if you do not use the <form> tag, the $.get method will still work and send and receives data from the PHP file.

After filling the text boxes, click the button “Create Account”. At the click event of the button, the entered data is sent to the PHP file by using jQuery $.get method:


 

(See the script section in <head> tag)

The get-forms.php file contains the code where I used the $_GET array to access that information. The information is assigned to PHP variables that are finally displayed by using the echo statement:


 

Generally, you may want to save this information into a database like MySQL. I am displaying this just for demonstration.

Also, note the information is not displayed directly by the echo statement. The PHP data is returned back to the $.get method of jQuery. And this line of code in the <script> section displayed the information:


 

You can see the complete code including markup (with Bootstrap CSS classes), jQuery code, and PHP script at the demo page.

This div height required for enabling the sticky sidebar