Jump to content

keeping a form populated if error


jeff5656

Recommended Posts

If I have a form, and I have aform processing page like process.php and let's say I want to redirect back to the form if there is an error (i.e. passwords don't match).

 

How can I redirect back to the form and retain the contents of the other fields, so the user doesn't have to type it all over again? 

 

BTW I know I can do this with java on the actual form.php page, but was wondering if there was a PHP solution)

Link to comment
Share on other sites

The easiest method is if you put your form processing code and your form on one page.

 

The easiest method, if you are using two different pages, would be to use $_SESSION variables so that the values are available between pages.

Link to comment
Share on other sites

along those same lines, you can also leave the processing page as is but just move the validation logic to the form page. Then when validation passes do an include of the processing page. If the validation logic is the same, then just do an include of that page in the appropriate place on the form pages as well. A modular design will save you a ton of time.

 

Here is an example script

<?php
//Set some needed vars
$errorMsg = '';
$name  = '';
$email = '';
$age   = '';
    
if(isset($_POST['name']))
{
    //Form was submitted, process data
    $name  = trim($_POST['name']);
    $email = trim($_POST['email']);
    $age   = trim($_POST['age']);
    
    //Validate data
    $errors = array();
    if(empty($name)) { $errors[] = " - Name is required."; }
    if(empty($email)) { $errors[] = " - Email is required."; }
    
    //Check for errors
    if(count($errors)>0)
    {
        //There were errors, prepare error message
        $errorMsg  = "The following errors occured:<br />";
        $errorMsg .= implode("<br />\n", $errors);
    }
    else
    {
        //There were no errors, include processing page
        include("processData.php");
        exit();
    }
}
    
?>
<html>
<body>
<div style="color:red;">
<?php echo $errorMsg; ?>
</div>
<br /><br />
    
Enter your data
<form name="test" action="" method="post">
<label for="name"><b>Name:</b></label>
<input type="text" name="name" id="name" value="<?php echo $name; ?>" />
<br />
<label for="email"><b>Email:</b></label>
<input type="text" name="email" id="email" value="<?php echo $email; ?>" />
<br />
<label for="age">Age:</label>
<input type="text" name="age" id="age" value="<?php echo $age; ?>" />
<br />
* Required fields are in bold
<br /><br />
    
<button type="submit">Submit</button>
</form>
</body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.