PHP Program to read value from the HTML form using GET method and print the value


November 18, 2021, Learn eTutorial
1177

How to pass values from HTML to PHP using GET Method in PHP?

The GET method is used to pass the values from the HTML form to the PHP. The get method passes the value through the URL. In this program, we are passing the first name and the last name of the user from the HTML page using the get method. For that, we have to assign an attribute name in the tag of the input field and assign the value to it and we also have to give the name attribute to the submit button also and in the PHP script we have to check the condition is the submit button clicked by using the built-in function isset() with $_GET['submit'](get method with the name of the submit button) as the argument. And after that, we have to read the first name and last name by using the $_GET method with the corresponding name and assign it to the variables. After that, we can print the value of those variables.

We have to run this program on localhost.

ALGORITHM

Step 1: Check condition 'isset($_GET[''sibmit])' if true perform the following steps

Step 2: Assign the value of '$_GET[['fName']' into the variable fName

Step 3: Assign the value of '$_GET[['lName']' into the variable lName

Step 4: Print the values of the variable $fName and $lName

PHP Source Code

                                          <!DOCTYPE html>
<html lang="en">

<head>
    <title>Document</title>
</head>

<body>
    <form method="get">
        <label for="fName">Enter your first name: </label>
        <input type="text" name="fName">
        <label for="lName">Enter your second name: </label>
        <input type="text" name="lName">
        <input type="submit" value="Enter" name="submit">
    </form>
    <?php
    if (isset($_GET['submit'])) {
        $fname = $_GET['fName'];
        $lName = $_GET['lName'];
        echo "Hello! $fname $lName Welcome to learnetutorials.com";
    }
    ?>
</body>

</html>
                                      

OUTPUT

Hello! Jhon Doe Welcome to learnetutorials.com