0

I'm wondering why this ajax call, passes the div id instead of the button one that I want to.

            <?php
           while($info = mysqli_fetch_array($sql))
           {

            echo "<tr>

             <th>" .$info['name']. "</th>
             <th>" .$info['pass']. "</th>
             <th><a href=\"http://" .$info['link']. "\">" .$info['link']. "</a></th>
              <th><div class=\"delbuttons\" id='5'> <button  data-toggle=\"modal\" data-target=\"#myModal\"  id='" .$info['id'].  "'> Delete </button> </div> </th> </tr> ";

           }
           ?>
     </table>
     <script>
        $(document).ready(function(){
            $('.delbuttons').click(function() {
                $.ajax({
                type: 'get',
                url: 'ajax.php',
                data: { 
                    row: this.id
                },
                success: function(msg){
                    if (msg)
                        alert(msg);

                    location.reload();
                }
                });
            });
        });
     </script>

I've set the button id to 5 just for debugging, and as I expected it returned "5" instead of the button's id.

I've tried both, get & post.

Thanks in advance!

Mattia
  • 5,909
  • 3
  • 26
  • 41

1 Answers1

3

You’re selecting $('.delbuttons'), so this refers to that <div>.

Either select $('.delbuttons button') instead, or use $(this).children(":first").attr("id") or some other way to get the first element child in jQuery instead of this.id.

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75