AJAX使用复选框删除多个记录

时间:2015-10-18 11:02:34

标签: javascript php jquery ajax

我正在使用jQuery和php执行多个删除记录操作,目前我可以通过点击其工作的复选框删除单个 / 多个记录截至目前很好,但每次删除记录时我的页面都会刷新,因为我没有使用ajax。

我是ajax的初学者我想使用 JQUERY / AJAX 执行相同的操作,每次删除我的记录时都不会重新加载我的页面所以我想使用ajax相同的代码,以便我可以处理我的页面重新加载。

有人帮助我实现它谢谢!!

HTML / PHP

   <form method="post" name="data_table">
            <table id="table_data">
                 <tr> 
                <td>Name</td>
                <td>Select All <input type="checkbox" id="check_all" value=""></td>
            </tr>
            <?php 
                $query = mysql_query("SELECT * FROM `products`");
                while($row = mysql_fetch_array($query)) 
                {
            ?>
                    <tr> 
                        <td>
                            <?php echo $row['product_title']; ?>
                        </td>
                        <td>
                            <input type="checkbox" value="<?php echo $row['id'];?>" name="data[]" id="data">
                        </td>
                    </tr>
            <?php 
                } 
            ?>
            </table>
            <br />
            <input name="submit" type="submit" value="Delete" id="submit">
       </form>

JQuery的

  jQuery(function($) 
            {
            $("form input[id='check_all']").click(function() 
            {   
                var inputs = $("form input[type='checkbox']");
                for(var i = 0; i < inputs.length; i++) 
                { 
                    var type = inputs[i].getAttribute("type");
                    if(type == "checkbox") 
                    {
                        if(this.checked) 
                        {
                            inputs[i].checked = true;
                        } 
                        else 
                        {
                            inputs[i].checked = false;
                        }
                    } 
                }
            });

            $("form input[id='submit']").click(function() 
            {  var inputs = $("form input[type='checkbox']");
                var vals=[];
                var res;
                for(var i = 0; i < inputs.length; i++) 
                { 
                    var type = inputs[i].getAttribute("type");
                    if(type == "checkbox") 
                    {
                        if(inputs[i].id=="data"&&inputs[i].checked){
                            vals.push(inputs[i].value);
                        }
                    } 
                }

                var count_checked = $("[name='data[]']:checked").length; 
                if(count_checked == 0) 
                {
                    alert("Please select a product(s) to delete.");
                    return false;
                } 
                if(count_checked == 1) 
                {
                    res= confirm("Are you sure you want to delete these product?");   
                } 
                else 
                {
                    res= confirm("Are you sure you want to delete these products?");  
                }       
                if(res){
               /*** This portion is the ajax/jquery post calling   ****/
                $.post("delete.php", {data:vals}, function(result){
                    $("#table_data").html(result);
                 }); 
                }
            });
            });

PHP删除代码

 <?php
 if(isset($_POST['data'])) 
 {
      $id_array = $_POST['data']; // return array
      $id_count = count($_POST['data']); // count array

      for($i=0; $i < $id_count; $i++) 
      {
         $id = $id_array[$i];
         $query = mysql_query("DELETE FROM `products` WHERE `id` = '$id'");
         if(!$query) 
         { 
              die(mysql_error()); 
         }
     }?>

1 个答案:

答案 0 :(得分:1)

请将更改jquery作为

jQuery(function($) 
{
$("form input[id='check_all']").click(function() 
{   
    var inputs = $("form input[type='checkbox']");
    for(var i = 0; i < inputs.length; i++) 
    { 
        var type = inputs[i].getAttribute("type");
        if(type == "checkbox") 
        {
            if(this.checked) 
            {
                inputs[i].checked = true;
            } 
            else 
            {
                inputs[i].checked = false;
            }
        } 
    }
});

$("form input[id='submit']").click(function() 
{  var inputs = $("form input[type='checkbox']");
    var vals=[];
    var res;
    for(var i = 0; i < inputs.length; i++) 
    { 
        var type = inputs[i].getAttribute("type");
        if(type == "checkbox") 
        {
            if(inputs[i].id=="data"&&inputs[i].checked){
                vals.push(inputs[i].value);
            }
        } 
    }

    var count_checked = $("[name='data[]']:checked").length; 
    if(count_checked == 0) 
    {
        alert("Please select a product(s) to delete.");
        return false;
    } 
    if(count_checked == 1) 
    {
        res= confirm("Are you sure you want to delete these product?");   
    } 
    else 
    {
        res= confirm("Are you sure you want to delete these products?");  
    }       
    if(res){
   /*** This portion is the ajax/jquery post calling   ****/
    $.post("delete.php", {data:vals}, function(result){
        $("#table_data").html(result);
     }); 
    }
});

});

删除.php为

 <?php
 if(isset($_POST['data'])) 
 {
      $id_array = $_POST['data']; // return array
      $id_count = count($_POST['data']); // count array

      for($i=0; $i < $id_count; $i++) 
      {
         $id = $id_array[$i];
         $query = mysql_query("DELETE FROM `test` WHERE `id` = '$id'");
         if(!$query) 
         { 
              die(mysql_error()); 
         }
     }?>
     <tr> 
                <td>ID</td>
                <td>TITLE</td>
                <td>Select All <input type="checkbox" id="check_all" value=""></td>
            </tr>
            <?php 
                $query = mysql_query("SELECT * FROM `test`");
                while($row = mysql_fetch_array($query)) 
                {
            ?>
                    <tr> 
                        <td>
                            <?php echo $row['id']; ?>
                        </td>
                        <td>
                            <?php echo $row['name']; ?>
                        </td>
                        <td>
                            <input type="checkbox" value="<?php echo $row['id'];?>" name="data[]" id="data">
                        </td>
                    </tr>
            <?php 
                } unset($row);

}