1

I write up a code to delete my articles within a table from the database.everthing is working fine but when i see the requests from firebug NET tab it shows me 11 requests instead of one .is there any wrong of my code ? I have attached my firebug NET tab result here

   //delete form start...........
            $("#pard_admin").on("click", ".deleteArticle", function (event) {

                event.preventDefault();
                var data = $(this).closest('tr').find('td:first').html();



                $.ajax({

                    url: "../pard_site/view/delete.php",
                    type: "POST",
                    data: "deleteId=" + data,

                    success: function (response) {
                        $("#pard_admin").load('../pard_site/view/articles.php');

                    }
                });




            });
            //delete form end...........

enter image description here

curious_coder
  • 2,392
  • 4
  • 25
  • 44
Mr PHP
  • 33
  • 7

1 Answers1

0

You should see two requests :

  • one for $.ajax()
  • one for load()

The most likely cause of multiple requests is that the delegated click handler is attached multiple times. It needs to be attached once, not once per row.

You may be able to reduce the two requests to one as follows :

var $pard_admin = $("#pard_admin").on("click", ".deleteArticle", function(event) {
    event.preventDefault();
    var $row = $(this).closest('tr');
    $.ajax({
        url: "../pard_site/view/delete.php",
        type: "POST",
        data: { "deleteId": $row.find('td:first').text() },
        success: function() {
            $row.remove();//or similar, depending on exactly what should disappear from the interface on successful delete.
        }
    });
});
Beetroot-Beetroot
  • 18,022
  • 3
  • 37
  • 44
  • var $pard_admin why this ? – Mr PHP Jun 15 '13 at 10:31
  • Good point. The assignment is not really necessary. It's just a means of keeping a reference to `$("#pard_admin")` in case it's needed elsewhere. This saves having to rediscover the DOM node, which is "expensive" especially in a large DOM. – Beetroot-Beetroot Jun 15 '13 at 10:35
  • So how would i call that var when i need it.do i want to add another jquery part for it ? – Mr PHP Jun 15 '13 at 10:39
  • Elsewhere in your code you can use `$pard_admin` in place of `$("#pard_admin")`. The only provisos are that (a) the assignment must have been made and (b) that `$pard_admin` is within scope of the statement which seeks to use it. Please don't get hung up on this - it's a side issue and I probably should have omitted the assignment from my answer. – Beetroot-Beetroot Jun 15 '13 at 10:45
  • Do you understand everything else? – Beetroot-Beetroot Jun 15 '13 at 11:07