如何通过AJAX传递多个复选框值,并处理它们?| [英] How to Pass Multiple CheckBox values through AJAX and process them?|

查看:217
本文介绍了如何通过AJAX传递多个复选框值,并处理它们?|的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

晚上好,大家好,

我想通过AJAX来传递多个复选框值和一个外部的.php脚本处理。目标:删除使用复选框多行而无需刷新页面

请帮助我通过选择复选框进datastring并把他们在mysql命令在外部PHP文件。这是code到目前为止:

该复选框:

 <表格名称=frmMainID =myForm的方法=邮报的OnSubmit =返回onDelete();>


<输入类=checkbox_button_del类型=提交ID =buttondel值=删除/> //向阿贾克斯


<输入类型=复选框级=CB-元素NAME =chkDel []ID =chkDel< = $ I;>中值='($ id)的。'>


<输入类型=隐藏名称=hdnCount值=< = $ I;?>>
< /形式GT;
 

在AJAX:

  $(函数(){
    $(checkbox_button_del)。点击(函数(){
        VAR ID = $(本).attr(ID);
        VAR dataString ='ID ='+ ID; //通复选框IDS不知何故
        VAR父母= $(本)。家长(TR:第一');


        $阿贾克斯({
            键入:POST,
            网址:核心/动作/ delete_multiple.php
            数据:dataString,
            缓存:假的,

            成功:函数(){
                parent.fadeOut('300',函数(){
                    $(本)上卸下摆臂();
                });
                $(#显示)。负载(display.php)

            }
        });

        返回false;
    });
});
 

该删除脚本:

  //接收复选框IDS从阿贾克斯和删除行

    为($ i = 0; $ I<计数($ _ POST [chkDel]); $ I ++)
    {
        如果($ _ POST [chkDel] [$ i]!=)
        {
            $ STRSQL =DELETE FROM球员;
            。$ STRSQL =WHERE ID ='。$ _ POST [chkDel] [$ i]。';
            $ objQuery =请求mysql_query($ STRSQL);
        }
    }
 

解决方案

您可以通过将所有的复选框的形式做到这一点。然后添加下面一行在你的函数:

  datastring = $('#MyForm的')序列化()。
 

Good evening guys,

I'm trying to pass multiple checkbox values through AJAX and process them with a external .php script. Goal: delete multiple rows using checkboxes without refreshing the page.

Please assist me with passing the selected checkboxes into the datastring and putting them in the mysql command in the external .php file. This is the code so far:

The checkboxes:

<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>

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

The delete script:

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

解决方案

You can do this by putting all the checkboxes in a form. And then add the following line in your function:

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

这篇关于如何通过AJAX传递多个复选框值,并处理它们?|的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆