Avoid double registrations in Javascript

1

I have had problems with saving the data to the database, I have a form where I have to select the area, amount and concept to enter overtime, by clicking on "continue" a modal is opened showing the employees that belong to the selected area in a table, each row has a checkbox and only those that I have selected must be saved, when I select them I click on the "confirm" button. Until then everything is fine, I saved the data to the database without any problem.

But when I open the modal and I do not do any action, I close it and open it again and I click on confirm "The records are saved twice", or when I click on confirm, it saves me, the modal is closed and when selecting another 'area and I confirm "again", they are saved several times.

Here is all the code I have because I have not found what the error was:

This is the html and the Javascript

I hope my question is clear

    
asked by Fabian Sierra 12.08.2016 в 22:09
source

1 answer

0

The cause of the problem is quite similar to this other question . I'm going to put a reduced version of the code you have in JSFiddle here:

$('#upload').on('click', function(){

  // código y operaciones
  // abrir modal
  // más código y operaciones

  confirm.on('click', function(){
    // pedir confirmación
    // serializar la tabla
    // petición AJAX y cerrar modal
  });

});

The button with ID "upload" is responsible for opening the modal, and the button with ID "confirm" is that it performs the AJAX request and closes the modal.

As the association of the event handler click of the "Confirm" button is done within the click event of the "upload" button, this means that a controller will be associated with the "confirm" button every time click on the "upload" button.

Let's see it step by step:

  • Start of page, the event handler click is associated with the "upload" button
  • The user clicks on the "upload" button
  • Modal opens
  • A controller of the event click is associated to the button "Confirm" (I will call it Confirm1)
  • The user does what he has to do and closes the modal by pressing "confirm"
  • The Confirm1 driver that makes the AJAX call is launched
  • The user keeps working and press the "upload" button again
  • Modal opens
  • An event handler click is associated with the "Confirm" button (I will call it Confirm)
  • The user does what he has to do and closes the modal by pressing "confirm"
  • The Confirm1 driver that makes the AJAX call is launched
  • The Confirm2 driver that makes the AJAX call is launched
  • The user keeps working and press the "upload" button again
  • Modal opens
  • A controller of the event click is associated with the button "Confirm" (I will call it Confirm3)
  • The user does what he has to do and closes the modal by pressing "confirm"
  • The Confirm1 driver that makes the AJAX call is launched
  • The Confirm2 driver that makes the AJAX call is launched
  • The Confirm3 driver that makes the AJAX call is launched
  • ....
  • Do you see what is happening? Each time you click on "upload" the same controller that makes the AJAX call is associated again and that is why you end up saving the data multiple times (as many times as you have clicked on "upload").

    The solution: as the modal already exists on the page and what is done is to show / hide it, you do not need to associate the controller of the click to "confirm" each time the modal is opened, it is enough that you do it once at the top of the page:

    $('#upload').on('click', function(){
    
      // código y operaciones
      // abrir modal
      // más código y operaciones
    
    });
    
    confirm.on('click', function(){
      // pedir confirmación
      // serializar la tabla
      // petición AJAX y cerrar modal
    });
    

    Try this and tell me how you are doing.

        
    answered by 12.08.2016 / 22:53
    source