-2

Every now and then you need a working example and I keep getting confused between POST & GET requests.

Armin Nehzat
  • 398
  • 5
  • 12

2 Answers2

1

The $_POST variables are variables that is used when submitting data from a form inside a "single page" while $_GET variables are variables you can "pass to another page by the URL", thus enabling other .php page to use your variable through the $_GET variables.

There also exist the $_REQUEST that can be use to obtain data from a form for both $_POST and $_GET variables.

Kiel Labuca
  • 1,195
  • 8
  • 13
0
<?php
// Check if action is set
if(isset($_POST["action"]))
{
    switch($_POST["action"])
    {
        case "number_submit" :
            // Submission from the number submit form
            header("Location: ".$_SERVER["PHP_SELF"]."?number=".$_POST["number"]);
            die();
        default :
            die("Unknown action : ".$_POST["action"]);
            break;
    }
}
?>
<html>
<head>
    <title>Self Submit</title>
</head>

<body>
    <?php
    if(isset($_GET["number"]))
    {
        // Display the number if it is set.
        ?>
        Here is the number : <?php echo ($_GET["number"]); ?><br />
        <a href="<?php echo $_SERVER["PHP_SELF"]; ?>">Click here to enter another number..</a>
        <?php
    } else {
        // Display the form
        ?>
        <form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
        <input type="hidden" name="action" value="number_submit" />
        Please enter a number : <input type="text" name="number" />
        <input type="submit" value="Submit" />
        </form>
        <?php
    }
    ?>
</body>
</html>
Armin Nehzat
  • 398
  • 5
  • 12