0

I am having a problem that is driving me crazy. I have looked for solutions on the net but everything I have found has not worked for me and I do not know what else to try.

I have a page in PHP that obtains data from a DB and with it dynamically creates a table. At the end of each row there is a button or link to delete the corresponding record.

This is the part of the PHP:

<div class="row mt-3">
<div class="col-12 col-xl-12">
    <div class="card border-card-tab" id="reload-table">
        <div class="card-header pb-0">
            <h5 class="card-title">Restaurants List</h5>
        </div>
        <div class="card-body pt-0">
            <table class="table table-striped table-hover" id="restaurants-table">
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Id</th>
                        <th>Address</th>
                        <th>City Id</th>
                        <th class="text-end">Phone</th>
                        <th class="text-end">Email</th>
                        <th class="text-end">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                        // Code which obtain the table data from MySql database
                        //SELECT bla, bla,2 bla3 FROM blatable... etc.
                    ?>
                    <tr class="table-row-link">
                        <td class="Update"><i class="fas fa-circle text-success fa-fw"></i>Eat at Jhon's</td>
                        <td class="Update">123456789A</td>
                        <td class="Update">First Avenue</td>
                        <td class="Update">Heaven</td>
                        <td class="Update text-end">01233445567</td>
                        <td class="Update text-end">info@restaurantfake.com</td>
                        <td class="table-action text-end">
                            <a class="Delete" href="../Model/_restaurantsDBHelper.php?button-action=button-logicaldelete-restaurant&tab=1&inputIdRestaurant=1" data-id="1" rel="#reload-table" >Delete</a>
                        </td>
                    </tr>
                    <tr>
                        ...
                        ...
                        ... etc.
                    </tr>
                </tbody>                      
                <tfoot>
                </tfoot>
            </table>
        </div>
    </div>
</div>

The elimination is carried out by calling a PHP page through ajax that, once the deletion has been carried out, returns a string "ok" or "ko" indicating the result and in the success of the ajax a popup with a message is displayed while it is reloaded the board without the deleted row.

This is the JS code:

$(function () {
    $("a[class='Delete']").click(function (e) {
        var id = $(e.currentTarget).data('id');
        var urlDel = $(e.currentTarget).attr('href');
        var reload = $(e.currentTarget).attr('rel');
        $.ajax({
            type:'post',
            url: urlDel,
            data:'&dataid='+id,
            success:function(msg){   
                $(reload).load(location.href + " " + reload);             
                if ((typeof msg != "undefined") && (msg != null) && (msg == 'ok')){
                    showMessage("Deleted", "Data with id: "+id+"</strong> has been deleted.");
                } else {
                    showMessage("Error", "Data with id: <strong>"+id+"</strong> has not deleted.");
                }
            }
        });
        return false;
    });
});

The problem is this: when I access the page for the first time (it doesn't matter which browser I use), everything works fine, but when I click on the delete button after the reload, the "click" event is no longer recognized and in instead the PHP page returns and shows the result (the text "ok" or "ko"). The ajax no longer works.

I have read that this could be due to the fact that when reloading the table the code no longer "listens" to the "click" event and for this it is necessary to modify the function in a similar way to this:

$(function () {
    $("reload-table]").on("click", "a[class='Delete']", function (e) { ...etc. same code above});


$(function () {
    $("body").on("click", "a[class='Delete']", function (e) { ...etc. same code above});
    

$(function () {
    $(document).on("click", "a[class='Delete']", function (e) { ...etc. same code above});

Basically I have tried all the solutions indicated in the solution of the question: JQuery rebinding on vs bind after AJAX query but nothing seems to work. Can anybody see what I'm doing wrong?

T.S.D.
  • 1
  • 5
  • Does this answer your question? [Jquery click event not working after append method](https://stackoverflow.com/questions/15420558/jquery-click-event-not-working-after-append-method) – hassan Nov 10 '21 at 12:51
  • also you can check my answer her https://stackoverflow.com/a/69394486/2359679 – hassan Nov 10 '21 at 12:51
  • 1
    Have you tried using event delegation? Event delegation allows you to attach a single event listener, to a parent element, that will fire for all descendants matching a selector, whether those descendants exist now or are added in the future: https://learn.jquery.com/events/event-delegation/ – esQmo_ Nov 10 '21 at 13:06
  • Wait why r u reloading the page?, while ajax is not meant for that. and you are using anchor tag too. – Mohammed Khurram Nov 10 '21 at 14:50
  • Hi everyone, Thank you very much for the quick reply! I have already tried to use event delegation and I think that the solution is indeed that. But I'm doing something wrong because it doesn't work. I think I am not adding the parameters correctly (I'm a newbie with this). so I can't find the correct way to implement the function. – T.S.D. Nov 10 '21 at 22:17
  • This is my last attempt so far: `$ ("#reload-table"). on ("click", "a [class = 'Delete']", function (e) { ` – T.S.D. Nov 10 '21 at 22:26
  • Make it another way round, declare a function and call the function on the button onclick. – Zeff Tang Nov 11 '21 at 06:59

1 Answers1

0

I have applied the solutions that you have suggested and others that I have found on the net but none have worked.

I think the problem is in the elements that I am passing to ajax.

Anyway, since what I want to achieve is to "hide" the row that the user deletes, I have realized that it is not necessary to reload the table since it is enough to delete the row, so it looks like this:

[...]

success: function (msg) {
   // I changed this ----> $ (reload) .load (location.href + "" + reload);
   $ ('# rowid _' + id) .remove (); // <---- For this one

[...]

Thank you very much everyone for the help!

T.S.D.
  • 1
  • 5