【问题标题】:Delete multiple entries with AJAX使用 AJAX 删除多个条目
【发布时间】:2012-02-16 21:55:04
【问题描述】:

我可以使用 AJAX 一次删除一个条目。现在我可以使用复选框选择条目并同时删除它们,这在没有 AJAX 的情况下效果很好。现在我正在尝试使用 AJAX 删除多个条目。这是我目前所拥有的:

ajax:

`$(function() {
$(".checkbox_button_del").click(function() {
var id = $(this).attr("id");
var dataString = 'id='+ id ;
var parent = $(this).parents('tr:first');

                        var notice = '<div class="notice">'
                                  + '<div class="notice-body">' 
                                      + '<img src="core/displays/notify/delete-icon.png" alt="" />'
                                      + '<h3>Deleted item</h3>'
                                      + '<p>The item has been succesfully deleted.</p>'
                                  + '</div>'
                                  + '<div class="notice-bottom">'
                                  + '</div>'
                              + '</div>';

                        $ny( notice ).purr(
                            {
                                usingTransparentPNG: true
                            }
                        );
$.ajax({
type: "POST",
url: "core/actions/delete_multiple.php",
data: dataString,
cache: false,

success: function()
{
parent.fadeOut('300', function() {$(this).remove();});
    $("#load_tasklist").load("core/statistics/stats_brackets.php")
    $("#load_tweets").load("response.php")
    $("#load_mod_day_summary").load("core/displays/mod_day_summary.php")
    $("#load_mod_task_selector").load("core/displays/mod_task_selector.php")
}
});

return false;
});
});`

外部删除脚本:`

include('../../core/additional/connect-db.php');

    for($i=0;$i<count($_POST["chkDel"]);$i++)
    {
        if($_POST["chkDel"][$i] != "")
        {
            $strSQL = "DELETE FROM players ";
            $strSQL .="WHERE id = '".$_POST["chkDel"][$i]."' ";
            $objQuery = mysql_query($strSQL);
        }
    }



    header('Location: ' . $_SERVER['HTTP_REFERER']);`

以及带有删除按钮的结果列表:`

<form name="frmMain" id="myForm" method="post" OnSubmit="return onDelete();">


<?php 

 $connection = mysql_connect($server, $user, $pass)  or die ("Could not connect to server ... \n" . mysql_error ());
 mysql_select_db($db) or die ("Could not connect to database ... \n" . mysql_error ());

    $result = mysql_query("SELECT * FROM players WHERE (userid = '$username' AND done = '0' AND measure = 'task' AND day = '$user_mydate') ORDER BY id DESC")
                    or die(mysql_error());

     echo '<div id="checkbox_button_div">
    <input class="checkbox_button_del" type="submit" id="buttondel" value="Delete" />
    <input class="checkbox_button_done" type="submit" id="buttondone" value="Done" />
    <input class="checkbox_button_done" type="submit" id="buttonfavorite" value="Favorite" />
    <input class="checkbox_button_done" type="submit" id="buttonmove" value="+1 day" />
    </div>';

     echo '

            <table id="tableOne" cellpadding="0" cellspacing="0" width="760" border="0" class="yui">    
        <thead>

            <tr>                                    

                <th><a href="#" title="Click Header to Sort">Task</a></th>

                <th><a href="#" title="Click Header to Sort">Minutes</a></th>
                <th><a href="#" title="Click Header to Sort">Time</a></th>

                <th><a href="#" title="Click Header to Sort">Category</a></th>
                <th> <input type="checkbox" class="check" value="check all" /></th>

            </tr>

        </thead>
     <tbody> ';    

        $i = 0;
        while($row = mysql_fetch_array( $result )) {   
        $i++;
            include ('core/additional/params_tasks.php');

            echo ' 

            <tr class="handcursor" onmouseover="' .($mouseover). '" onmouseout="' .($mouseout). '">         

                <td class="editable" id="' .($id). '" width="180">' .($task). ' </td>
                <td class="editable" id="' .($id). '">' .($minutes). '</td>
                <td onClick="' .($onclick). '">' .($hours_start). '.' .($minutes_start). ' - ' .($hours_due2). '.' .($minutes_due2). ' </td>
                <td onClick="' .($onclick). '">' .($categorie). ' </td>         
                <td align="center"><input type="checkbox" class="cb-element" name="chkDel[]" id="chkDel<?=$i;?>" value="' .($id). '"></td>

            </tr> ';

            }
            // close table>
            echo '</tbody>
        <tfoot>
                <tr style="display:none;">
                <td style="border: 0px;" colspan="4">
                    No matching results..
                </td>
            </tr>           
        </tfoot>
     </table>

      '; 

     ?>

    <input type="hidden" name="hdnCount" value="<?=$i;?>">

    </form>`

我认为 AJAX 脚本也应该传递“$i”值,但我不知道该怎么做。如果您不清楚问题,请告诉我。期待您的回答!

【问题讨论】:

  • FireBug 是否给您任何错误?
  • 问题不太清楚,有什么问题?
  • 您好,AJAX 正在执行(并给出成功消息),但没有删除任何内容。我的问题是:我应该如何修改 AJAX 脚本以传递正确的表单信息?谢谢:)
  • 为什么不从复选框中创建一个 ID 数组。将其传递给您的脚本,并使用 - delete from table where id in(....) 删除记录?它应该足够简单。
  • 听起来不错的解决方案,可惜我还不够熟练,还不能自己写,你能帮我吗(如果这不会消耗太多时间的话)?:)

标签: php mysql ajax


【解决方案1】:

你的 jquery 会是这样的。

$("#Submit1").click(function () {

            var arr = new Array();


            $("input:checked").each(function () {

                arr.push($(this).attr("id"));

            }); //each

            $.ajax({
             type: "POST",
       url: "core/actions/delete_multiple.php",
       data: arr ,//pass the array to the ajax call
       cache: false,

       success: function()
             {   }
       });//ajax

            }); //each


        }); //click

因为 PHP 函数会得到一个 JSON 对象。你需要做一个 JSON 解码来获取数组.. 见 JSON decode... 使用这个数组来创建你的查询,比如..

   delete from SalesLT.Customer
where CustomerID in (1,2,3,4);

【讨论】:

  • 无法让它工作:(需要一些关于 json 的帮助(docu 页面离线 btw)你能帮我重写 delete_multiple.php 文件吗?(只要它花费几分钟! ) 在此先感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-26
  • 1970-01-01
  • 1970-01-01
  • 2015-02-21
  • 2016-12-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多