PHP $_GET method

Explain the $_GET method in PHP with an example?

You can use $_GET global variable to retrieve data from HTML forms which have its method attributes value as 'get'. The $_GET variable contains value of all the form elements in an array format name of the form element become key and its value will be value of the form element which is sent by the HTTP GET method. Whatever information user fill in forms and sent to server it is visible to all, you can see this information in current URL. And you can send limited amount of information through $_GET method. The example is mentioned below: Code


<html>
<body>
  <form action="myPHPScript.php" method="get" name="myform">
    Enter your first name: 
    <input type="text" name="txtFirstName" id="txtFirstName" />
    
Enter your last name: <input type="text" name="txtLastName" id="txtLastName" />
Enter your address: <input type="text" name="txtAddress" id="txtAddress" />
<input type="submit" name="save" id="save" value="Save"> </form> </body> </html>
In above mentioned example HTML page have three input fields and a submit button. When a user fills this form and click on the 'Save' button, then the form data will be sent to the "myPHPScript.php" PHP file, it is because we set action attribute as myPHPScript.php file, if you want to receive data on any other file then set your file name as action attribute's value. And method attribute set as 'get'. Now in below mentioned example we shown the code of myPHPScript.php file and how can we access forms value through $_GET. When a user clicks the "Save" button, the URL sent will look similar to below mentioned url:
Code

http://www.tutorial.com/welcome.php?txtFirstName=Peter&txtLastName=fedderer
&txtAddress=US
The "myPHPScript.php" file looks like this:
Code

<html>
  <body>
    Welcome Mr. .
Your address is . </body> </html>
The output of the above mentioned example:

Welcome Mark.
Your address is US.

FAQ 1. Where does PHP GET method data appear after form submission? The submitted information appears in the browser's address bar after the question mark (?) as URL parameters. 2. Can we pass multiple values using the GET method? Yes. Multiple values can be passed using key-value pairs separated by the ampersand (&) symbol. 3. Is it possible to bookmark a page containing GET parameters? Yes. Since GET parameters are included in the URL, users can bookmark or share the URL with its query parameters.