Open In App

How to get form data using POST method in PHP ?

PHP provides a way to read raw POST data of an HTML Form using php:// which is used for accessing PHP’s input and output streams. In this article, we will use the mentioned way in three different ways. We will use php://input, which is a read-only PHP stream.

We will create a basic HTML form page where we can use all the possible approaches one at a time.
HTML Code:




<!DOCTYPE html>
<html>
<head>
    <title>POST Body</title>
    <style>
        form {
            margin: 30px 0px;
        }
        input {
            display: block;
            margin: 10px 15px;
            padding: 8px 10px;
            font-size: 16px;
        }
        div {
            font-size: 20px;
            margin: 0px 15px;
        }
        h2 {
            color: green;
            margin: 20px 15px;
        }
    </style>
</head>
<body>
    <h2>GeeksforGeeks</h2>
    <form method="post">
        <input type="text" name="username"  
                           placeholder="Enter Username">
        <input type="password" name="password"
                               placeholder="Enter Password">
        <input type="submit" name="submit-btn" 
                             value="submit">
    </form>
    <br>
</body>
</html>

Below examples illustrate the approach:
Example 1: In this example, we will use file_get_contents() Function. The file_get_contents() function is used to get the data in string format.

Example 2: In this example, we will use print_r() Function. It is a much simpler way to get the POST data and that is using the print_r() function. This will give the output in the form of an array.

Example 3: We can also use var_dump() function which will give us an array as well but with a bit more information.


Article Tags :