Save form data using AJAX to PHP

前端 未结 2 1029
忘掉有多难
忘掉有多难 2021-01-24 14:08

How can I save the form data in a file or a local db (maybe using AJAX) which send the data via form action to an external db?

The source code for my form is here: http:

2条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-24 14:32

    LocalStorage would be your best bet. I would suggest using storejs as their API is straight forward, easy to use, and x-browser.

    You could then trigger the form values to be stored on the "blur" event of each field.

    $('input').on('blur', function (e) {
      var el = e.target;
      store.set(el.attr('name'), el.val());
    });
    

    When you are ready to submit to the server, you could use something like the following:

    $('#formID').on('submit', function (e) {
      e.preventDefault();
      $.post('/my/save/route', store.getAll(), function () { ... });
    });
    

    You of course could do all of this without storejs and use vanilla JS to interact with the native LocalStorage API.

提交回复
热议问题