Sticky form

Count 1 (via hidden input)


Code follows

<?php

error_reporting
(E_ALL);    // set error reporting to all

// pull the variables out of the POST array with ternary operators

$name  = isset($_POST['name'])  ? $_POST['name']  : '';
$phone = isset($_POST['phone']) ? $_POST['phone'] : '';

// filter all input taken from the Browser

$name  = htmlentities($name);
$phone = htmlentities($phone);

// a hidden counter, just to show the form submitting to itself

$hiddenVar = isset($_POST['hiddenVar']) ? (int)$_POST['hiddenVar'] : 0;
$hiddenVar++;

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Sticky form</title>
    <link rel="stylesheet" href="css/styles.css">
</head>
<body>

<div class="page">

    <p class="nav"><a href="../index.php">Home</a></p>

    <h1>Sticky form</h1>

    <form action="sticky-form.php" method="POST">

        <label for="name">Name</label>
        <input type="text" id="name" name="name" size="36" value="<?php echo($name); ?>">

        <label for="phone">Phone</label>
        <input type="text" id="phone" name="phone" size="36" value="<?php echo($phone); ?>">

        <input type="hidden" name="hiddenVar" value="<?php echo($hiddenVar); ?>">

        <input type="submit" name="submit" value="Submit">

    </form>

    <p>Count <?php echo($hiddenVar); ?> (via hidden input)</p>

    <?php

    
// show the results underneath, but only after the form has been submitted

    
if (isset($_POST['submit']))
    {
        
?>

        <h2>POST Results</h2>

        <pre><?php print_r($_POST); ?></pre>

        <?php
    
}

    
?>

    <hr>

    <h2>Code follows</h2>

    <div class="code"><?php highlight_file(__FILE__); ?></div>

    <p class="nav nav-foot"><a href="../index.php">Home</a></p>

</div>

</body>
</html>