Jump to content

How to save form data in case form validation fails


AdRock

Recommended Posts

I have a form which is validated but I would like to know how i can temporarily store the form data in case a user forgets to enter their email address for example.

I find it really annoying after filling out loads of form details that if i forget a field then the whole lots if wiped off and I have to start again.

Do i have to store the form data in a cookie and if so how do i do it and delete it when the form has been sent successfully?
Hmmmm... well, assuming you are just doing a normal form to a php page, you could have a meta refresh on the second page that gets tripped if data is missing (etc), that would include all the current data witht he url.  Assuming the form page is .php.  There is also a way to send post data through a link, so you might be able to send it back without cluttering up the url bar.  It would also be good to include a variable to show which field is missing, so you can tell the user.  You would also have to have the gorm page insert the variables (GET or POST) into the form fields as their value.

Does that help at all?  :)

-Alex
Even though it isn't PHP, many sites just use javascript for form validation. Then the user isn't waiting for a page load for each validation. Of course, you still want to keep your PHP validation as a failsafe, for people that have javascript off, or bypass it.


I'm not as experienced as most of the people helping here, but couldn't you just use the GET/POST data to fill the forms as default values?
The easiest and simplest way to do this is to have the verification in the same script as your form. Upon entering the script, check if the form had been submitted. If script was invoked because the form had been submitted, then do the verification.

If the verification fails, redisplay the form setting the value of each field to the posted value.

Ken
This is the form part of my page.  Has anyone any ideas how I can refill the fields with what was previously entered by the user such as their email address if they forgot to put that in before clicking submit?

I have heard about people doing it with javascript but i also read somewhere that it can be done with php.

[code]<p class="style3">Email us at: <a class="two" href="mailto:[email protected]?subject=Website%20Feedback">[email protected]</a></p>
<p class="style3">Please fill in this form if you have any queries or suggestions.</p>
<form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">

    <p class="style3"><label for="txtName">Name:</label>
    <input type="text" title="Please enter your name" name="txtName" size="30" value="<? echo $firstname .' '.$lastname; ?>" /></p>

    <p class="style3"><label for="txtEmail">Email:</label>
    <input type="text" title="Please enter your email address" name="txtEmail" size="30" value="<? echo $emailaddress; ?>"/></p>

    <p class="style3"><label for="txtMessage">Comments:</label>
    <textarea title="Please enter your message" name="txtMessage" rows="10" cols="30"></textarea></p>

    <p class="style3">For security purposes, please enter the image shown in the text box below.<br>If you have trouble reading the image, refresh the page to display a new one.</p>
   
    <p class="style3"><label for="captcha"></label>
    <div class="captcha"><img src="/includes/captcha.php" alt="captcha image"></div></p>

    <p class="style3"><label for="verify">Image text:</label>
    <input type="text" title="Please enter the image text" name="verify" id="verify" size="9"/></p>

    <p class="style3"><label for="submit">&nbsp</label>
    <input type="submit" value="Send" class="submit-button"/>

</form>
<?php
}
elseif (empty($name) || empty($email) || empty($message) || empty($_POST['verify']) && $_POST['verify'] == $_SESSION['captchstr']) {

    echo $empty_fields_message;
}
else {

    // Stop the form being used from an external URL
    // Get the referring URL
    $referer = $_SERVER['HTTP_REFERER'];
    // Get the URL of this page
    $this_url = "http://".$_SERVER['HTTP_HOST'].$_SERVER["REQUEST_URI"];
    // If the referring URL and the URL of this page don't match then
    // display a message and don't send the email.
    if ($referer != $this_url) {
        echo "You do not have permission to use this script from another URL.<br>";
    echo "If you are behind a firewall please check your referrer settings.";
        exit;
    }

    // The URLs matched so send the email
    mail($your_email, $subject, $message, "From: $name <$email>");

    // Display the thankyou message
    echo $thankyou_message; 
}
?>[/code]
Your PHP should really BEFORE your form.

And use the IF Submit Button Set process form, else display the form, and if submit fails then clear the submit var and re-display the form, then you could use the ternary operator like

[code=php:0]<input type="text" name="username" size="24" maxlength="24" value="<?php if (isset($_POST['username'])) echo $_POST['username']; ?>" />[/code]

in your form, which would show pre-set vars from previous submit or empty if no submit happened :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.