使用Jquery和PHP删除MySQL行


Remove MySQL rows using Jquery and PHP

我一直在一点一点地将我正在开发的一个PHP web应用程序转换为Jquery AJAX表单提交。

我目前正在处理的是删除表行。我认为这是我的javascript的一个问题,因为即使使用return false;,它也会不断刷新页面。这是我的表单创建对象的问题,但多亏了这个网站的一些帮助,我把它整理好了,现在它完全可以工作了。

这是我的javascript:

$("form#table_customer").submit(function() {
        var query_string = 'page=customer&rows=';
        $(":checked").each(function() {
            query_string += this.value + "-";
        });
        $.ajax({
            url: "inc/deletesql.php",
            type: "POST",
            data: query_string,
            cache: false,
            success: function(data){
                $('#maincontent').load('inc/showObjects.php?o=customer');
            }
        });
        return false;
    });

PHP文件(deletesql.PHP)可用于正常的POST提交。但我似乎无法通过ajax发布数据。

您可以在http://www.sfyfe.com/studioadmin/index.php?i=customer

    $('#maincontent').load('inc/showObjects.php?o=customer');
    ...
    // at this point form#table_customer doesn't exist yet in the DOM
    // so this binding a handler will give no effect
    $("form#table_customer").submit(function() {
           ....
    });

试试这个:

 // make the load reusable
 function addDeleteHandler() {
     // handle the load event
     // attach the event handler everytime the content is loaded, 
     // and the form#table_customer is added to the dom
     $('#maincontent').load('inc/showObjects.php?o=customer', function() {
         $("form#table_customer").submit(function() {
             var query_string = 'page=customer&rows=';
             $('input[name="checkbox"]:checked').each(function() {
                query_string += this.value + "-";
             });
             $.ajax({
                 url: "inc/deletesql.php",
                 type: "POST",
                 data: query_string,
                 cache: false,
                 success: function(data){
                   addDeleteHandler();
                 }
             });
             return false;
          });
      });
  }
  addDeleteHandler();