Question

I have a form that users can use to submit a report. As one of the fields is a text area where they can place quite a long text, I would like to insert a function that autosaves the entry every minute or so, but I'm not sure about the principles behind this.

the solution I can think of is:

  1. Autosave the current entry in the database as a version (eg. Entry_id = 1, Version = 1, Visibility = FALSE)
  2. After 1 minute, autosave again as Entry_id = 1, Version = 2, Visibility = FALSE)
  3. Keep repeating the above until either:
  4. User presses "SAVE". In that case, change the visibility of the last version to TRUE, and remove all other versions (all entries where entry_id == 1 and Visibility == FALSE)
  5. User chooses not to save ("DISCARD"). In this case I remove all entries where entry_id == 1 and visibility == FALSE

two considerations:

  • The above should also work well if the customers is editing an existing entry.
  • I can't use entry_id as primary key anymore

This seems to work in my mind but I'm wondering if I'm using a very convoluted process, and there is a better one?

Thanks, P

Was it helpful?

Solution

Once the form is loaded a function will be triggered and it will repeat itself after every 10 seconds (or whatever time you mention). That function will take your form, serialize it and send to a server side script (most probably PHP) and that PHP script will insert that record in database (through AJAX) and return the primary key (id) of this inserted record. Now when function executed for the first time it inserted the record with following parameters:

Entry_id = 1 (or whateverthe primary key at which the record is saved), 
Version = 1, 
Visibility = FALSE

This function returned a primary key. So now you'll have to put a check at start of your autosave function that checks whether the primary key is returned or not (record inserted) and if this key has been returned then it will again get your form serialize it and sends to another function in your server side script (Update function) along with the primary key returned from first autosave call as a parameter and updates the record against this key in database.

Now user keeps on typing the content and autosave function keeps on firing after every 10 seconds, and data is being inserted (for the first time) and updated constantly. As soon as the user presses save button on the form, again take that primary key and update that record with these values:

Version = 1, 
Visibility = TRUE
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top