-1

I want to submit the form data to the database, 2 seconds after the user has entered the data in the form

I am using ajax function to accomplish this.but form gets submitted when a press a single key

$('#eid').bind('input propertychange', function() {
  $.ajax({
    method: "POST",
    url: "intime2.php",
    data: { content: $("#eid").val() }
  }).done(function( msg ) {
    alert( "Data Saved: " + msg );
  });
});

When I enter the employee ID=12 in the form. I expect the result to be stored in database as 12 but the result stored is 1. before pressing 2 the data is submitted. I also want the form to be refreshed automatically for every consecutive submissions.

My goal: employees should enter their employee id(I am going to take inout from card reader, but for now I like to use timeout function) after entering the form should auto submit without them pressing the submit button then the form should refresh and next employee should enter his employee ID

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • 3
    Auto-submitting is always a bad idea for this reason. How long do you delay before submission? What happens if someone is typing a value and halfway through stops to check it and correct it? Then you've submitted an invalid value. What happens if someone types quickly yet you've put a 5 second delay on? You're making them wait for absolutely no reason, which is really annoying. I'd suggest you use a button as is standard practice. – Rory McCrossan Apr 04 '19 at 09:18
  • I am in learning process. I like to add timeout function to this code. So after entering the required data then the form has to be submitted. thank you – Sathesh Kumar Apr 06 '19 at 06:30
  • In which case research `setTimeout()`. Also `bind()` is deprecated, use `on()` instead, and make sure you're using the latest version of jQuery. – Rory McCrossan Apr 06 '19 at 07:55
  • can u explain me how to use the on() in code – Sathesh Kumar Apr 06 '19 at 08:16
  • See the documentation: http://api.jquery.com/on/ – Rory McCrossan Apr 06 '19 at 08:47

1 Answers1

-1

Use debounce function to submit your form data to server

References: Debounce function in jQuery https://davidwalsh.name/javascript-debounce-function

faizulhaque
  • 106
  • 4
  • While these links may be helpful it's best to include the relevant parts within your answer, as the links may rot and the answer become useless. Also note that if you believe this question has already been answered elsewhere on this site, please vote to close it as a duplicate. – Rory McCrossan Apr 04 '19 at 09:20