PHP Get [An Associative Array]

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 variables become keys.

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

<form method=”get” action = “”>

<input type="text" name="email">

</form>

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

$get_email=$_GET[“email”];

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:

http://localhost/test.php?email=abc%40gmail.com

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

http://localhost/test.php?email=abc%40 gmail.com&gender=Male

(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 the 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 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 the form’s get method.
  • The target file is the same as 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 below:

PHP GET

Online demo and code

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

<form method=”get” action=””>

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

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

$get_name=$_GET["getname"];

$get_gender=$_GET["getgender"];

$get_email=$_GET["getemail"];

$get_password=$_GET["getpassword"];

$get_address=$_GET["getaddress"];

if( $get_name )

{

echo "Name entered is: ". $get_name ."<br>";

echo "Gender:" .$get_gender ."<br>";

echo "Email:" .$get_email ."<br>";

echo "Password:" .$get_password ."<br>";

echo "Address:" .$get_address ."<br>";

}

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 those used in the 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

This is how the link is created in the markup section:

<a href="?name=your name&gender=Female&email=abc@gmail.com">PHP $_GET test</a>

The following PHP script is used to access URL parameters:

<?php

$get_name=$_GET["name"];

$get_gender=$_GET["gender"];

$get_email=$_GET["email"];

if( $get_name )

{

echo "Name entered is: ". $get_name ."<br>";

echo "Gender:" .$get_gender ."<br>";

echo "Email:" .$get_email ."<br>";

}

?>

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 in 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

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 receive data from the PHP file.

After filling in 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 the jQuery $.get method:

$("#getdata").click(function(){



getname=$("#getname").val();

getgender=$("#getgender").val();

getemail=$("#getemail").val();

getpass=$("#getpassword").val();

getaddress=$("#getaddress").val();



$.get("get-forms.php", {name:getname, gender:getgender, email:getemail, password:getpass, address:getaddress },function(getresult){

$("#getdiv").html(getresult);

});


});

(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:

$get_name=$_GET["name"];

$get_gender=$_GET["gender"];

$get_email=$_GET["email"];

$get_password=$_GET["password"];

$get_address=$_GET["address"];


if( $get_name )



{

echo "Name entered is: ". $get_name ."<br>";

echo "Gender:" .$get_gender ."<br>";

echo "Email:" .$get_email ."<br>";

echo "Password:" .$get_password ."<br>";

echo "Address:" .$get_address ."<br>";

}

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 to the $.get method of jQuery. This line of code in the <script> section displayed the information:
$("#getdiv").html(getresult);

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

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we unravel the mysteries of coding together!