繁体   English   中英

将数组发布到/通过ajax到php

[英]Posting an array to/thru ajax to php

所以我有一个从php循环创建的数组,如下所示:

echo '<input type="checkbox" name="types[]" id="types[]" value="' . $id . '"> ' . $type . '<br>';

虽然它可以将其传递给php,但我想进行一些更新并将其传递给ajax以传递给php,这样我就可以让ajax在页面上为我做一个漂亮的回报。

我尝试了所有类型的方法来将其传递给Ajax并传递给php,但没有任何效果。

这是ajax代码:

jQuery(document).ready(function(){

    $('#contactform').submit(function(){

        var action = $(this).attr('action');

        $("#message").slideUp(750,function() {
        $('#message').hide();

        $('#submit')
            .after('<img src="assets/ajax-loader.gif" class="loader" />')
            .attr('disabled','disabled');



        $.post(action, {
            types['types[]']: $('#types').val(),
            form_name: $('#form_name').val(),
            site_id: $('#site_id').val()
        },
            function(data){
                document.getElementById('message').innerHTML = data;
                $('#message').slideDown('slow');
                $('#contactform img.loader').fadeOut('slow',function(){$(this).remove()});
                $('#submit').removeAttr('disabled');
                if(data.match('success') != null) $('#contactform').slideUp('slow');

            }
        );

        });

        return false;

    });

});

我试图使它正常工作,因为该网站的其余部分使用相同的功能,并且我想保持相同的方式。 我不想在已经创建的页面上删除其他ajax内容。

因此,任何见解都将受到赞赏。

谢谢

编辑/更新:

因此,在设置了像Meager这样的行之后,解释了:

echo '<input type="checkbox" name="types[]" class="types" value="' . $id . '"> ' . $type . '<br>';

然后将ajax代码调整为:

    $.post(action, {
        form_name: $('#form_name').val(),
        site_id: $('#site_id').val(),
        types: $('.types').map(function(){ $(this).val(); });

    },

它似乎只是绕开了ajax,并使用以下命令直接发布到我的process.php文件中:

Array
(
    [types] => Array
        (
            [0] => 20
            [1] => 21
            [2] => 22
        )

    [site_id] => 112
    [form_name] => prd_svc
    [Submit] => Submit
)

==========================================

jQuery(document).ready(function(){

$('#contactform').submit(function(){

    var action = $(this).attr('action');

    $("#message").slideUp(750,function() {
    $('#message').hide();

    $('#submit')
        .after('<img src="assets/ajax-loader.gif" class="loader" />')
        .attr('disabled','disabled');



    $.post(action, {
        form_name: $('#form_name').val(),
        site_id: $('#site_id').val(),
        types: $('.types').map(function(){ $(this).val(); });

    },
        function(data){
            $('#message').html(data);
            $('#message').slideDown('slow');
            $('#contactform img.loader').fadeOut('slow',function(){$(this).remove()});
            $('#submit').removeAttr('disabled');
            if(data.match('success') != null) $('#contactform').slideUp('slow');

        }
    );

    });

    return false;

});

});

您尝试通过以下方式创建元素的“数组”: id="types[]"是错误的。 您不能以这种方式对元素进行分组,如果可以的话,仍然不会返回值数组: $('#types').val()

您必须为每个元素分配一个唯一的ID,或者为它们提供一个共享的类,然后通过该类进行选择:

echo '<input type="checkbox" name="types[]" class="types" value="' . $id . '"> ' . $type . '<br>';

然后,要将一组选定的元素转换为其值,可以使用map

// Collect all values
$('.types').map(function () { $(this).val(); });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM