0

I'm trying to automatically save a form when leaving a page with JavaScript by using the following code

$(window).unload( function() {
    $("#registration-patient-form").submit();
});

Yet, somehow the form does not get submitted and I'm simply redirected to the page I'm navigating to. Any idea on how to accomplish this?

ftramnitzke
  • 151
  • 9
  • If the user wants to leave your site, he should be able to. Use localStorage to store the values everytime they are changed, and add a sync.js to every page that stores these values onload/on a button click etc... – Jonas Wilms Sep 14 '16 at 13:21
  • Onbeforeunload should be used to notify the user to may submit the form/ complete sth on the page and not to run additional code – Jonas Wilms Sep 14 '16 at 13:22

1 Answers1

0

You should do it every x seconds. Because user can close the page at unexpected time. 1) Create a function and call it every 1 second. 2) Check the data if it is changed. 3) If data is changed, submit

cemsina güzel
  • 390
  • 4
  • 14