So there is an order form on the site with a large number of fields (already optimized and still the number is quite large).

Task: When the user is bored with filling in the data, he clicks a cross or closes the browser - save all his entered form data somewhere (in a session using PHP, and it looks like this is the only available option).

Purpose: After some time has passed, the user returns to the site, opens the form, and the form is almost full with the data that he did not fill out last time, thereby speeding up the entire process of ordering products.

Features: The form works by means of JQuery and is sent to the AJAX server by request, and, accordingly, the order is thereby completed. The browser does not cache such forms, and after reloading the page again you have to fill out.

  • Not sure, so I’m not even writing as an answer, but it may turn out like this: function saveData () {// code} window.onunload = saveData; - Deonis
  • cross-browser " local storage (userdata in IE) " Further - approximately as wrote @knes. Personally, I am not a supporter of cookies. confused poor limit - Zowie

2 answers 2

Not all browsers support, or work correctly with the page closing event. But even if you go to implement this "feature" for some browsers, then in my opinion you can do it like this: - serialize the form into a string - write the array into a cookie for a long time

Thus, when the user returns to the page, you can restore the data from the cookie. When using jquery, use serialize ();

The implementation provided by the user @knes will help you dispense with the event of closing the page.

  • Yes, I searched the search engine and found many negative opinions about this browser event, so I’ll probably have to play with setTimeout () and other application functions. - Chetson

Use COOKIES with "infinite" storage time. There are no other reliable ways to authorize to save the form. Saving when you lose focus from any field.

$("input").blur(function(){/*Saving to cookies*/}); 
  • It so happened that a handler is already on BLUR - it accepts input.val () in the validation engine with the jQuery Validator plugin (standard plugin). I think you can implement this process as approximately done in Yandex mail. In it, after a certain period of time, the textarea field is backed up somewhere to the server, respectively, you can restore it, and write SessionID in the cookie so that you can restore the session data. - Chetson
  • Save the data with MySQL and put the session ID with the key, and when the user logs in again, as if by magic from Scule, all the data was restored. And yet, the very essence BEFORE authorization and AFTER will still be the same. Technically, I see it like this, but advice and optimization is vital. - Chetson