PHP $_POST superglobal

PHP $_POST
  • PHP $_POST is a PHP variable which is used to collect form data after submitting an HTML form with method="post". $_POST is also widely used to pass variables.


  • example
  • <html>
    <body>
    <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
    Name: <input type = "text" name = "name" />
    <input type = "submit" />
    </form>

    <?php
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = $_POST['name'];
    if (empty($name)) {
    echo "Name is empty";
    } else {
    echo "Welcome ".$name;
    }
    }
    ?>
    </body>
    </html>

    Output:

    Welcome amit