All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

PHP Get URL Parameters

Last Updated : Mar 11, 2024

PHP Get URL Parameters

In this article we will show you the solution of PHP get URL parameters, we used a super global variable called $_GET in the PHP file. $_GET: this variable is used to collect data after submitting the form

Here, by clicking on the HTML link we can get the URL parameter. We will use one example using HTML form to get URL parameters by using $_GET method.

Step By Step Guide On PHP Get URL Parameters :-

In this example, we will get input from the user to get the URL parameter.

HTML Code:

<!DOCTYPE html>
<html lang = " en " >
<head>
    <meta charset = " UTF - 8" >
    <meta http-equiv = " X-UA-Compatible " content = " IE=edge " >
    <meta name = " viewport " content = " width = device-width , initial-scale = 1.0 " >
    <title> php get url parameters </title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css">
</head>
<body>
    <h1 style=" color : rgb(113, 221, 113) ;"> TALKERSCODE </h1>
    <h2> php get url parameters </h2>
    <a href="get_method.php?firstname=akash&lastname=roy"> Click Here! </a>
    <form action="get_method.php">
         <label for="name"> FIRST NAME </label>
         <input type="text" name="firstname"> <br>
         <label for="name"> LAST NAME </label>
         <input type="text" name="lastname"> <br>
        <input type="submit" value="submit">
        </form>
</body>
</html>
  1. First, we write <! DOCTYPE html> which we used as the instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As mentioned above, the <head> tag contains information about the web page. In this tag, a <title> tag is used which helps us to specify a webpage title.
  4. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  5. Attach an external CSS file using <link> tag
  6. Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
  7. <h1> tag used to add heading here and also adding the inline CSS here.
  8. Create a hyperlink using <a> tag with the ‘href’ of the php file
  9. Creating a form using <form> tag. Set the action of the php file name.
  10. Two <input> tags for the input for the URL parameters, as we use <label> for the first name and the last name. and also create a <input> for the submit button with the value ‘submit’.
  11. Close the HTML tag with </html>

PHP code

<?php
$firstname = $_GET['firstname'] ;
$lastname = $_GET['lastname'] ;
echo 'First Name: '.$firstname. '<br>' ;
echo 'last Name: '.$lastname. '<br>' ;
?>
  1. At first <?php tag to write the php.
  2. First create two variables for the URL parameters as in here $firstname and $lastname.
  3. Using $_GET method with the name of the URL parameter.
  4. Using echo to display it on the HTML page. By clicking on the link we can get the URL parameter.
  5. Close the php code with ?> tag

Conclusion :-

At last, here in conclusion, here we can say that with this article’s help, we know how to get URL parameters using PHP.

I hope this article on PHP get URL parameters helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪