Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用ajax发送多个复选框数据_Javascript_Php_Jquery_Ajax_Checkbox - Fatal编程技术网

Javascript 使用ajax发送多个复选框数据

Javascript 使用ajax发送多个复选框数据,javascript,php,jquery,ajax,checkbox,Javascript,Php,Jquery,Ajax,Checkbox,我有一个表格,其中以复选框格式列出了要插入数据库的国家。比如说: <input type="checkbox" name="country[]" value="Afghanistan" checked>Afghanistan<br> <input type="checkbox" name="country[]" value="Albania" checked>Albania<br> <input type="checkbox" name="c

我有一个表格,其中以复选框格式列出了要插入数据库的国家。比如说:

<input type="checkbox" name="country[]" value="Afghanistan" checked>Afghanistan<br>
<input type="checkbox" name="country[]" value="Albania" checked>Albania<br>
<input type="checkbox" name="country[]" value="Algeria" checked>Algeria<br>
<input type="checkbox" name="country[]" value="American Samoa" checked>American Samoa<br>
<input type="checkbox" name="country[]" value="Andorra" checked>Andorra<br>
<input type="checkbox" name="country[]" value="Angola" checked>Angola<br>
<input type="checkbox" name="country[]" value="Anguilla" checked>Anguilla<br>

无论我选择一个国家还是所有国家,我都会得到这个错误响应。如何使用ajax发送这些复选框数据?

您需要在输入捕获时将
.data()
切换到
.val()
。同时将输入的名称更改为
input[name=country\\[\\]]:选中

# Use "e" as the event
$("#submit").click(function(e) {
    # Use this function to stop the form
    e.preventDefault();
    # You could make a quick function to save all the countries
    # You can actually feed other selectors into this as well, so
    # it can do all your value retrievals, not just for countries
    function getCountries(checkboxes)
        {
            var getVals =   [];
            $.each($(checkboxes),function(k,v) {
                getVals.push($(v).val());
            });

            return getVals; 
        }

    var dataString = {
        clicks: $("#clicks option:selected").val(),
        # Run the function to gather
        # Note the name (escaped [] with :checked)
        country: getCountries("input[name=country\\[\\]]:checked")
    };

    $.confirm({
        title: 'Confirm!',
        content: 'Are you sure you want to purchase this advertisement?',
        buttons: {
            confirm: function () {
                $.ajax({
                    type: "POST",
                    dataType: "json",
                    url: "add-ptc-process.php",
                    data: dataString,
                    cache: true,
                    beforeSend: function(){
                        $("#submit").hide();
                        $("#loading-rent").show();
                        $(".message").hide();
                    },
                    success: function(json){
                        setTimeout(function(){
                            $(".message").html(json.status).fadeIn();
                            $('#mywallet').html('$' + json.deduct);
                            $("#loading-rent").hide();
                            $("#submit").show();
                        },1000);
                    }
                });
            },
            cancel: function () {
                $.alert('<span style="font-size: 23px">Purchase Cancelled!</span>');
            }
        }
    });
});
#使用“e”作为事件
$(“#提交”)。单击(功能(e){
#使用此函数停止窗体
e、 预防默认值();
#你可以做一个快速的功能来拯救所有的国家
#实际上,您也可以将其他选择器输入到该文件中
#它可以进行所有的价值检索,而不仅仅是针对国家
功能getCountries(复选框)
{
var getVals=[];
$。每个($(复选框),函数(k,v){
getVals.push($(v.val());
});
返回getVals;
}
变量数据字符串={
单击:$(“#单击选项:选定”).val(),
#运行函数以收集
#注意名称(转义为[]并带有:选中)
国家:getCountries(“输入[name=country\\[\\]]:选中”)
};
美元。确认({
标题:“确认!”,
内容:“您确定要购买此广告吗?”,
按钮:{
确认:函数(){
$.ajax({
类型:“POST”,
数据类型:“json”,
url:“添加ptc process.php”,
数据:dataString,
是的,
beforeSend:function(){
$(“#提交”).hide();
$(“#加载租金”).show();
$(“.message”).hide();
},
成功:函数(json){
setTimeout(函数(){
$(“.message”).html(json.status.fadeIn();
$('#mywallet').html('$'+json.decrete);
$(“#加载租金”).hide();
$(“#提交”).show();
},1000);
}
});
},
取消:函数(){
$.alert('采购已取消!');
}
}
});
});
试试看

然后将其发布到ajax数据:countries或更改代码以仅发送选中的元素

您的PHP将收到一个数组,以便您可以进行检查

if(is_array($_POST['countries'])){
         foreach($_POST['countries'] AS $country_name => $is_checked){
                  //do something...
         }
}

您可能需要使用
$(“input[name=country”).val()
而不是
$(“input[name=country”).data(“value”)
.data()
假设存在一个
data
属性,在这种情况下,
data value=“something”
是您没有的。您可能只想
序列化()
name='country[]'!=name='country'@Lixus还说了我要说的,你可能需要发送
“input[name=country\\[\]]”,所以你不能使用常规表单提交的原因是什么
我在这个链接上得到了sbczk的一个答案,我想这会起作用。看起来是这样。但是如何在我的代码中实现它给了我一个问题,因为我不太擅长ajax…有人可以查看这个答案并帮我在我的ajax代码中实现它吗?这个答案假设你的表单中没有其他复选框,名为其他东西se它将包括那些作为
country[]
复选框的一部分。如果这些是唯一的复选框,那么这个答案就可以了。
# Use "e" as the event
$("#submit").click(function(e) {
    # Use this function to stop the form
    e.preventDefault();
    # You could make a quick function to save all the countries
    # You can actually feed other selectors into this as well, so
    # it can do all your value retrievals, not just for countries
    function getCountries(checkboxes)
        {
            var getVals =   [];
            $.each($(checkboxes),function(k,v) {
                getVals.push($(v).val());
            });

            return getVals; 
        }

    var dataString = {
        clicks: $("#clicks option:selected").val(),
        # Run the function to gather
        # Note the name (escaped [] with :checked)
        country: getCountries("input[name=country\\[\\]]:checked")
    };

    $.confirm({
        title: 'Confirm!',
        content: 'Are you sure you want to purchase this advertisement?',
        buttons: {
            confirm: function () {
                $.ajax({
                    type: "POST",
                    dataType: "json",
                    url: "add-ptc-process.php",
                    data: dataString,
                    cache: true,
                    beforeSend: function(){
                        $("#submit").hide();
                        $("#loading-rent").show();
                        $(".message").hide();
                    },
                    success: function(json){
                        setTimeout(function(){
                            $(".message").html(json.status).fadeIn();
                            $('#mywallet').html('$' + json.deduct);
                            $("#loading-rent").hide();
                            $("#submit").show();
                        },1000);
                    }
                });
            },
            cancel: function () {
                $.alert('<span style="font-size: 23px">Purchase Cancelled!</span>');
            }
        }
    });
});
var countries = {};
$('input[name^="country"]').each(function(){
          countries[$(this).attr('value')] = $(this).is(":checked");
});
if(is_array($_POST['countries'])){
         foreach($_POST['countries'] AS $country_name => $is_checked){
                  //do something...
         }
}