Trying to have functionality to where when user clicks on trash can icon, a modal pops up asking them if they're sure if they want to delete or not. If they hit yes, a delete action is committed.
However, my problem that I'm having a hard time figuring out is passing my id from my function that has the parameter containing the userid to my other anonymous function event listener that listens for whether the delete button was clicked on or not.
I can't put the event listener anonymous function inside of the deleteUser function because this causes multiple delete actions to happen.
For example, 1st delete will work fine, I delete again, 2 deletes will happen at once, I delete again 3 deletes will happen at once.
here is an example of what I tried and won't work because it will cause many delete actions to happen at once.
function deleteUser(userid) {
deleteButton.addEventListener('click', function(userid){
var deleteData = userid;
// Delete the record
$.ajax({
method: 'POST',
url: 'delete-user-action.php',
data: deleteData,
success: function(){
console.log('user was successfully deleted');
}
})
})
}
and I understand why it's happening because the event listener to attaching itself every time the function is invoked. So I decided to move it outside like this:
function deleteUser(userid) {
console.log('i have access to id in this function');
}
deleteButton.addEventListener('click', function(userid){
// How can I get access to userid from deleteUser function?
var deleteData = userid;
// Delete the record
$.ajax({
method: 'POST',
url: 'delete-user-action.php',
data: deleteData,
success: function(){
console.log('user was successfully deleted');
}
})
})
So my question is, how can i get access to deleteUser parameter userid
without causing multiple delete actions to happen?
also, here is my php code that I'm using on the server to get the id:
<i class="fas fa-edit" data-toggle="modal" data-target="#editModal" id="'.$item['mem_id'].'"></i> <i class="fas fa-trash" data-toggle="modal" data-target="#deleteModal" onclick='."deleteUser('".$item['mem_id']."')".' id="'.$item['mem_id'].'"></i>