1

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>
Curious13
  • 329
  • 2
  • 23
  • You definitely should have the function defined outside the event handler, as in your second snippet. As for how to access the id, the event handler automatically gets passed the event object, which contains a `target` property referencing the DOM node in which the event happened. Doesn't this allow you to find the id to be deleted? – Robin Zigmond Oct 02 '18 at 23:07
  • It does, however when the trash can icon is clicked, a modal pops up asking the user to confirm whether they want to delete the record. I don't want it to where the user just clicks the trash can icon and the record gets deleted, I wanted a confirmation message and only after the user hits "yes" on the delete button within the confirmation popup modal does the record get deleted. – Curious13 Oct 02 '18 at 23:52

2 Answers2

1

I recommend passing integer values from the dom to javascript by way of a data- attribute.

id attributes cannot start with a number. I don't recommend using purely numeric element ids -- they lose their context/meaning within the scope of the html document. data-id, for instance, can simply be an integer. e.g. ... data-id={$item['mem_id']} ...'

Once you have the $this element at your disposal, just use the $(this).data('id') value in your ajax call.

The added convenience here is that you won't need to strip any characters from the passed value (in js or php).

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • I'll look into this suggestion. – Curious13 Oct 02 '18 at 23:52
  • Here is a similar execution: https://stackoverflow.com/q/21926263/2943403 notice how the id value should be sent via `data` when POSTing. – mickmackusa Oct 03 '18 at 00:06
  • Thanks for this however, my problem isn't finding out how to pass the id of the event i clicked on which causes the delete, it's deleting the record after the user clicks on "yes" in the modal thats pops up after they click on the trash icon (which contains the id parameter). So what happens is the user clicks on trash icon, message pops up asking if they want to delete, if they hit yes it gets deleted. – Curious13 Oct 03 '18 at 00:09
  • In your success function: https://api.jquery.com/remove/ I can't see enough of your dom to offer specifics. – mickmackusa Oct 03 '18 at 00:17
  • It's okay I think im overthinking it. I'm just gonna delete the record when the user clicks on the trash icon. What my hope was to have a confirmation message indicating to the user if they are sure if they wanted to delete it. Only then would the record get deleted. Thank you for helping. – Curious13 Oct 03 '18 at 00:25
  • Yes, you should offer a confirmation box to be safe. https://stackoverflow.com/a/10462878/2943403 – mickmackusa Oct 03 '18 at 00:30
  • I'll come back to this tomorrow as I have a lot of code and only tried to show the relevant parts. – Curious13 Oct 03 '18 at 00:31
  • 1
    [*id* attributes](https://html.spec.whatwg.org/#the-id-attribute) can start with a number. :-) – RobG Nov 26 '20 at 22:42
  • Thank you for your helpful comment, I've edited my answer to strike out the false claim. @RobG – mickmackusa Nov 26 '20 at 22:45
0

First, you'll want to make use of Unobtrusive JavaScript and replace that onclick with an event listener, as you appear to already be in the process of doing.

Second, I note that you're outputting the ID as the id parameter for both the edit and deletion icons. This is invalid markup, and will cause problems. Instead, I suggest prefixing the action to the ID:

<i class="fas fa-edit" ... id="edit_<?php echo $item['mem_id']; ?>"> // edit_1
<i class="fas fa-delete" ... id="delete_<?php echo $item['mem_id']; ?>"> // delete_1

Now, considering you're outputting the userid in question to the id field of the element being clicked on, there's no need for the deleteUser function at all; simply grab the ID of the deletion element itself, with this.id:

var deleteButton = document.getElementById('delete_1');

deleteButton.addEventListener('click', function() {
  var deleteData = this.id;
  console.log(deleteData);

  /*
  $.ajax({
    method: 'POST',
    url: 'delete-user-action.php',
    data: deleteData,
    success: function() {
      console.log('user was successfully deleted');
    }
  })
  */
})
<i class="fas fa-edit" id="edit_1">Edit</i>
<i class="fas fa-trash" id="delete_1">Delete</i>
Obsidian Age
  • 41,205
  • 10
  • 48
  • 71
  • question in regards to this, the delete button is actually the event of the confirm delete button after the modal pops up after clicking the the trash can which contains the user id, my initial question was is it possible to somehow get the userid of the trash can icon to the listening event of the confirm delete button that is popped up with the modal? Because the deleteButton doesn't contain the parameter of the userid, the trashcan icon does. The delete button is the confirm button of the modal that pops up after the user clicks on the trash can icon. – Curious13 Oct 02 '18 at 23:48