临时存储用户表单数据的最佳方式,用于表单重新提交

Currently building out a form that pushes data to a third-party client. After pushing said form data I get a reply back from the third-party server saying something along the lines of 'invalid / valid' data. If valid all is perfect in the world and I alert the user with a confirmation message. If invalid I would like to key back the values said user inputted back into the form to further expedient the process of form re-submission.

What I'm looking for here is what would be the best / most secure resource to utilize storing this temporary data to key back into the form? Cookie? Database (then clear the database)? Some kind of global variable?

(ps. This is a pretty basic form Name, Email, Address, CC. I don't plan to store CC information in anything! This seems to be a accident waiting to happen if I did, unless someone has experience with storing/clearing such sensitive data!)

Thanks in advance!

You could also use a SESSION if you would like to store the data on the server side. The advantages are: 1. it is compatible with any cookie enabled browser. 2. You can clear the session data once the user has closed down the browser. This is not always the case with cookies (unless you set it that way).

I suggest using AJAX:

  1. User enters form data
  2. User clicks "Send" button
  3. JS prevents the normal submission of the form, and makes an AJAX connection to your php file, with the form.
  4. Your php file validates it and sends the response
  5. JS gets the response and shows an appropriate message to the user.

To learn how to use AJAX, go to https://developer.mozilla.org/en-US/docs/AJAX

You could use localStorage to save the form data?

You can use html5 webstorage pretty easily if you plan to work on IE8+

The supported list is pretty comprehensive http://www.html5rocks.com/en/features/storage