通过JQuery $ .post发布数组

时间:2011-06-01 18:13:29

标签: php jquery arrays post

所以我是使用JQuery的新手,更多的是.post和我想知道的是:

PHP允许我发布多个表单输入值的数组,如下所示:

<input type="text" name="array[]" />

我可以用JQuery帖子做同样的事情吗?如果是这样,语法是什么?

我目前对它的使用是这样的:

$.post('search_item.php', { unit: form.unit.value }...

非常感谢,

4 个答案:

答案 0 :(得分:2)

是的!

 $.post("test.php", { 'choices[]': ["Jon", "Susan"] });

回复评论

一种方法是映射它们

var choices  = $('input[name="choices[]"]').map(function() {
    return this.value;
}).get();
    // then post would look like
    $.post("test.php", { 'choices[]':choices });

答案 1 :(得分:1)

最简单的方法可能就是这样:

$.post('file.php',$('input[name=array[]]').serialize(),...

答案 2 :(得分:1)

以下是检查有效电子邮件的示例,但它为您提供了基本想法:http://www.jensbits.com/2009/10/04/jquery-ajax-and-jquery-post-form-submit-examples-with-php/

$(function(){
    $("#JqPostForm").submit(function(){
        $.post("process_form.php", $("#JqPostForm").serialize(),
        function(data){
            if(data.email_check == 'invalid'){

                    $("#message_post").html("<div class='errorMessage'>Sorry " + data.name + ", " + data.email + " is NOT a valid e-mail address. Try again.</div>");
            } else {
                $("#message_post").html("<div class='successMessage'>" + data.email + " is a valid e-mail address. Thank you, " + data.name + ".</div>");
                }
        }, "json");

        return false;

    });
});

上例中表单的ID为#JqPostForm。 serialize函数将所有表单字段放在PHP $_POST对象中,您可以在process_form.php页面(或用于表单处理的任何页面)上获取它们,就像表单已提交到该页面一样。例如,$_POST['email']

答案 3 :(得分:0)

.post是一种速记Ajax函数

http://api.jquery.com/jQuery.post/

相关问题