phphtmlformsinput-history

Remember input history


I am using a form that uses process page to verify and then process all info. If something is wrong or is missing the message tells the users to go back to the page and fix it. However, when they do it, all other information that they put in is not shown in the fields. How can I make it so whatever they put in will show.


Solution

  • The best way is to do the validation on the same page and only redirect to another page when that succeeded:

    <?php
        if (!empty($_POST['foo'])) {
            // validate
            if (/* valid */) {
                // save data somewhere
                header('Location: nextpage.php');
                exit();
            }
        }
    ?>
    
    <form action="samepage.php">
        <input name="foo" type="text" value="<?php if (!empty($_POST['foo'])) echo htmlentities($_POST['foo']); ?>">
        <?php if (/* foo was invalid */) : ?>
            <p class="error">Please fill in Foo!</p>
        <?php endif; ?>
    </form>
    

    Alternatively: use sessions.