Dani Carla
Dani Carla

Reputation: 85

Sqlite html5 Duplicating Records on Inserting

I have a user log table, which inserts a record to every user action, but sometimes it duplicates the inserted records.

my code is very simple

<script>
    db.transaction(function(tx) {
      tx.executeSql('INSERT INTO tab_log (date_occurrence,hour_occurrence,number_user,occurrence,obs_occurrence) VALUES (?,?,?,?,?)', [var_date, var_hour, var_number_user, var_occurrence, var_obs_occurrence]);
    
    });

  </script>

   <input type="button" name="btn_finish" id="btn_finish" value="Finish" onclick="this.blur();salvar();">

EDIT I have post full code. This code works perfectly, but sometimes doubles in the row of the log table

Has this ever happened to anyone? Does anyone have any idea what it might be?

Upvotes: 0

Views: 32

Answers (1)

lenilsondc
lenilsondc

Reputation: 9800

You are adding a click listener twice so to speak. There is a function carregado() which is called on window load event that binds an event listener to the finish button.

function carregado() {
    document.getElementById('btn_finish').addEventListener('click', salvar);
}

However, further on the html side, you are setting the onclick attribute of that button, making it call salvar() twice as onclick and addEventListener('click') both binds a different listeners to the button.

<input ... id="btn_finish" value="Finish" onclick="this.blur();salvar();">

What you can do to sort this out, is to remove the onlick attribute call to salvar() as it breaks your logic to bind the event only after finishing loading the page with carregado(), so the onclick part seems useless this case. Nevertheless, it will prevent the salvar() to be called twice.

Upvotes: 1

Related Questions