0

Is there a way to save the form data so that if the submit fails they don't have to retype everything? For some reason, I'm getting an error on my script with Chrome but not with FireFox. It doesn't always happen with Chrome, and it's not exactly the main problem. If they submit and then the page doesn't load, they lose everything they typed when they go back. I've thought of only one thing so far, but it doesn't seem practical. It would be to save the form data to cookies when they press submit.

Is there a better way?

asked Aug 24, 2011 at 5:35
5
  • You could do a session or just use ajax. Jquery makes it pretty easy to do ajax. It will let you know if the ajax failed or not. Commented Aug 24, 2011 at 5:39
  • @Matt: I'm quite a noob here. What do you mean do a session or use ajax? What is ajax? Commented Aug 24, 2011 at 5:43
  • 1
    You ever notice how a webpage does something without refreshing? That is ajax. It uses javascript to communicate with the server. It sends data without refreshing the page. with jquery it would be pretty simple. You can serialize the form and then send it to your server. There are methods you can use to check if it failed or not and you can do one thing or another. If it is successful you can redirect them to another page if you wanted or just print a message saying it was successful. You can see it here api.jquery.com/jQuery.ajax there is also .post() and .get(). Commented Aug 24, 2011 at 5:48
  • Also sessions are used in php. You can store data using sessions in php and later recall that information. This is used a lot in websites where you have to login or something. Ever notice how sometimes you see a checkbox that says remember me? the remember me creates a cookie, but if they don't want a cookie you can use sessions. Commented Aug 24, 2011 at 5:55
  • possible duplicate of Good design and flow for PHP - retain form values after submit Commented Aug 24, 2011 at 7:12

1 Answer 1

1

It depends on what you mean by "submit fails." If you mean that there is a server error, and they have to press 'back', it's hard.

However, if you mean fails as in a they-forgot-to-put-an-email kind of fails, here is one solution:

You can grab the previous values from the $POST or wherever and stick them back into the HTML tags. For instance:

<?php
$prevCustEmail = makeItASafeString($_POST['custEmail']);
?>
<input type="text" id="custEmail" name="custEmail" value="<?php echo $prevCustEmail; ?>" />

Edit: For the server thing, the cookie hack may be the best. Here is another hack idea: You could modify your 500 server error page to have a button on it that says "go back to form", which would only show up in the event that this $_POST data was just went. The button would actually be submitting an identical form where every is type=hidden. This data would then get transferred to the previous page and stuffed in as above.

A third hack would be to load the page in an iframe, and have the success page jump out of the iframe. The server error would not do that. Then use Javascript onSubmit to detect that something has gone wrong, or just hope that the user notices nothing happened and clicks "submit" again.

answered Aug 24, 2011 at 5:39
Sign up to request clarification or add additional context in comments.

1 Comment

@user Then you should fix your server. :o) Seriously.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.