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.