Repopulate form after validation error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jaymanson
    New Member
    • Oct 2006
    • 29

    Repopulate form after validation error

    Help - I'm a bit of a newbie when it comes to PHP

    I have a form (using POST) which my PHP script validates then returns to either a success or error page. The error page contains the form fields again for the user to resubmit their details. How do I repopulate these fields with their previous submission to save them typing it all in again (as it's likely they won't bother!)

    Thanks in advance :-)

    Jay
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    Here is a sample of a form that requires 2 fields, employee name and emplyee number. When one or both are not filled in, display error(s) and re-display the form with the already filled values shown.
    [php]
    <?php
    /*-----------------------------------------------------------------------------*
    * This form has 2 parts:
    * 1. the first time (_submit not in $_POST) the form is displayed
    * to the user requesting input data
    * 2. when $_POST['submit'] is set, the form has been filled in
    * and the variables in $_POST must be validated.
    * Fields filled: continue whatever processing you want to do
    * Fields not exist or blank, redisplay form with previous values echoed
    *-----------------------------------------------------------------------------*/
    if (isset($_POST["_submit"]) ) {
    $errors = array();
    // validate the input
    if (!isset($_POST['empl']) OR strlen(trim($_P OST['empl'])) == 0) {
    $errors[] = 'Employee number is invalid';
    }
    if (!isset($_POST['name']) OR strlen(trim($_P OST['name'])) == 0) {
    $errors[] = 'Employee name is invalid';
    }
    // further validation of inpuit
    //
    // ------------------------------------------------------
    // Everything is fine, continue processing
    // ------------------------------------------------------
    if (!$errors) {
    // when values valid process further
    exit;
    }
    // ------------------------------------------------------
    // Errors encounterd, display them and show form again
    // ------------------------------------------------------
    else {
    print '<span style="color:re d"><ul><li><b>' ;
    print implode('</b></li><li><b>',$er rors);
    print '</b></li></ul></span>';
    }
    } // End isset submit
    ?>
    <form name='form1' action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
    Employee number:&nbsp;&n bsp;
    <input type ='text' name='empl' maxlength='10' value="<?php echo (isset($_POST['empl'])) ? $_POST['empl'] : ""; ?>" /> <br />
    Employee Name:&nbsp;&nbs p;
    <input type ='text' name='name' maxlength ='30' value="<?php echo (isset($_POST['name'])) ? $_POST['name'] : ""; ?>" /><br />
    <input type ='hidden' name='_submit' value = '1' /> <br />
    <input type ='submit' name='submit' value="Submit request"/>
    </form>[/php]

    Ronald :cool:

    Comment

    • jaymanson
      New Member
      • Oct 2006
      • 29

      #3
      Cheers for that - I'm just off to bed but will try this out when I get home from work tomorrow!

      Will let you know how I get on!

      - Jay

      Comment

      Working...