AJAX 删除带有复选框的多条记录

问题描述 投票:0回答:1

我正在使用 jQuery 和 php 执行多个删除记录操作,目前我可以通过单击复选框来删除 single / multiple 记录,到目前为止它工作正常,但每次删除记录时我的页面都会刷新,因为我不使用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()); 
         }
     }?>
javascript php jquery ajax
1个回答
0
投票

请将 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);

}
© www.soinside.com 2019 - 2024. All rights reserved.