通过jQuery AJAX发送多个复选框数据到PHP() [英] Send multiple checkbox data to PHP via jQuery ajax()

查看:171
本文介绍了通过jQuery AJAX发送多个复选框数据到PHP()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要提交包含在我的网站一个textarea字段和输入字段(S)(类型=复选框与任意波形/可变数量的复选框)通过jQuery的阿贾克斯()一个POST表单。 PHP接收textarea的数据和Ajax响应正确显示给用户。然而,似乎在PHP没有收到该复选框数据(是对其进行检查,或没有)。我怎样才能得到这个工作?这里是code,我有:

中的HTML:

 <形式方法=邮报行动=myurl.phpID = myForm会>
    < textarea的ID =MyField的类型=文本名称=MyField的>< / textarea的>
    <输入类型=复选框名称=myCheckboxes []ID =myCheckboxes值=someValue1/>
    <输入类型=复选框名称=myCheckboxes []ID =myCheckboxes值=someValue2/>
    ...(也许有更多的复选框 - 动态生成必要时)
    <输入ID =提交类型=提交名称=提交值=提交的onclick =submitForm()/>
< /形式GT;
 

jQuery的:

 函数submitForm(){
$(文件)。就绪(函数(){
$(#形式myForm会)。递交(函数(){

        VAR myCheckboxes =新的Array();
        $(输入:选中)。每个(函数(){
           myCheckboxes.push($(本).VAL());
        });

        $阿贾克斯({
            键入:POST,
            网址:myurl.php
            数据类型:HTML,
            数据:{MyField的:$(textarea的[名称= MyField的])VAL()。
                    myCheckboxes:myCheckboxes},
            成功:功能(数据){
                $('#myResponse)。HTML(数据)
            }
        });
        返回false;
});
});
 

现在,在PHP

  $ MyField的=用htmlspecialchars($ _ POST ['MyField的']));
如果(使用isset($ _ POST ['myCheckboxes']))
{
    为($ i = 0; $ I<计数($ _ POST ['myCheckboxes']); $ I ++)
    {
        //做一些东西,保存到数据库等等。
    }
}
//创建响应
$响应='的HTML响应';
$响应=函数stripslashes($响应);
回声($响应);
 

一切都很正常:当表单提交一个新的记录存储在我的数据库,响应ajaxed回的网页,但该复选框的数据不会被发送。我想知道的复选框如果有的话,都被检查其中。我读过有关.serialize(),JSON等,但没有这已经奏效。我一定要序列化/ JSON在jQuery和PHP?怎么样?有个方法比另一种更好的与复选框的形式发送数据时?我一直停留在这2天。任何帮助将大大AP preciated。由于时间提前!

解决方案

  VAR myCheckboxes =新的Array();
$(输入:选中)。每个(函数(){
   。数据['myCheckboxes []']推($(本).VAL());
});
 

您正在推动复选框错阵列数据['myCheckboxes []'] 而不是 myCheckboxes.push

I want to submit a POST form that contains a textarea field and an input field(s) (type="checkbox" with an arbitrary/variable number of checkboxes) on my website via jQuery's .ajax(). PHP receives the textarea data and the ajax response is correctly displayed to the user. However, it seems that PHP is not receiving the checkbox data (was it checked, or not). How can I get this to work? Here is the code I have:

The HTML:

<form method="post" action="myurl.php" id=myForm>
    <textarea id="myField" type="text" name="myField"></textarea>
    <input type="checkbox" name="myCheckboxes[]" id="myCheckboxes" value="someValue1" />
    <input type="checkbox" name="myCheckboxes[]" id="myCheckboxes" value="someValue2" />
    ...(maybe some more checkboxes - dynamically generated as necessary)
    <input id="submit" type="submit" name="submit" value="Submit" onclick="submitForm()" />
</form>

The jQuery:

function submitForm() {
$(document).ready(function() {
$("form#myForm").submit(function() {

        var myCheckboxes = new Array();
        $("input:checked").each(function() {
           myCheckboxes.push($(this).val());
        });

        $.ajax({
            type: "POST",
            url: "myurl.php",
            dataType: 'html',
            data: { myField:$("textarea[name=myField]").val(),
                    myCheckboxes:myCheckboxes },
            success: function(data){
                $('#myResponse').html(data)
            }
        });
        return false;
});
});

Now, the PHP

$myField = htmlspecialchars( $_POST['myField'] ) );
if( isset( $_POST['myCheckboxes'] ) )
{
    for ( $i=0; $i < count( $_POST['myCheckboxes'] ); $i++ )
    {
        // do some stuff, save to database, etc.
    }
}
// create the response
$response = 'an HTML response';
$response = stripslashes($response);
echo($response);

Everything works great: when the form is submitted a new record is stored in my database, the response is ajaxed back to webpage, but the checkbox data is not sent. I want to know which, if any, of the checkboxes have been checked. I've read about .serialize(), JSON, etc, but none this has worked. Do I have to serialize/JSON in jQuery and PHP? How? Is one method better than another when sending form data with checkboxes? I've been stuck on this for 2 days. Any help would be greatly appreciated. Thanks ahead of time!

解决方案

var myCheckboxes = new Array();
$("input:checked").each(function() {
   data['myCheckboxes[]'].push($(this).val());
});

You are pushing checkboxes to wrong array data['myCheckboxes[]'] instead of myCheckboxes.push

这篇关于通过jQuery AJAX发送多个复选框数据到PHP()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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