【问题标题】:How to Pass Multiple CheckBox values through AJAX and process them?|如何通过 AJAX 传递多个 CheckBox 值并进行处理?
【发布时间】:2012-02-17 04:09:45
【问题描述】:

大家晚上好,

我正在尝试通过 AJAX 传递多个复选框值并使用外部 .php 脚本处理它们。目标:使用复选框删除多行而不刷新页面。

请协助我将选定的复选框传递到数据字符串中,并将它们放入外部 .php 文件中的 mysql 命令中。这是到目前为止的代码:

复选框:

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


<input class="checkbox_button_del" type="submit" id="buttondel" value="Delete" /> // submit to ajax


<input type="checkbox" class="cb-element" name="chkDel[]" id="chkDel<?=$i;?>" value="' .($id). '">


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

AJAX:

$(function () {
    $(".checkbox_button_del").click(function () {
        var id = $(this).attr("id");
        var dataString = 'id=' + id; //pass checkbox ids somehow
        var parent = $(this).parents('tr:first');


        $.ajax({
            type: "POST",
            url: "core/actions/delete_multiple.php",
            data: dataString,
            cache: false,

            success: function () {
                parent.fadeOut('300', function () {
                    $(this).remove();
                });
                $("#display").load("display.php")

            }
        });

        return false;
    });
});   

删除脚本:

// receive checkbox ids from ajax and delete rows

    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);
        }
    }

【问题讨论】:

标签: php mysql ajax


【解决方案1】:

您可以通过将所有复选框放在一个表单中来做到这一点。然后在您的函数中添加以下行:

datastring = $('#myform').serialize();

【讨论】:

    【解决方案2】:

    jQuery 内置了这个功能。

    见:http://api.jquery.com/serialize/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-19
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 2013-03-12
      • 1970-01-01
      • 1970-01-01
      • 2020-07-09
      相关资源
      最近更新 更多