PHP Program to read value from the HTML form using POST method


March 8, 2022, Learn eTutorial
1162

How to pass values from HTML to PHP using the POST Method in PHP?

The POST method is used to pass the values from the HTML form to the PHP. The post method won't pass 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 post 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 $_POST['submit'](post 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 $_POST 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($_POST[''sibmit])' if true perform the following steps

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

Step 3: Assign the value of '$_POST[['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="post">
        <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($_POST['submit'])) {
        $fname = $_POST['fName'];
        $lName = $_POST['lName'];
        echo "Hello! $fname $lName Welcome to learnetutorials.com";
    }
    ?>
</body>

</html>
                                      

OUTPUT

Hello! Jhon Doe Welcome to learnetutorials.com