dapperwaterbuffalo
dapperwaterbuffalo

Reputation: 2748

posting array through JQuery $.post

so I am new to using JQuery and more so .post and what I want to know is:

PHP allows me to post an array of multiple form input values like so:

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

so can I do the same with JQuery post? if so, what is the syntax?

my current use of it has been like so:

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

many thanks,

Upvotes: 2

Views: 2064

Answers (4)

jk.
jk.

Reputation: 14435

Here's an example that checks for a valid email but it gives you the basic idea: 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;

    });
});

The id of the form in the example above is #JqPostForm. The serialize function puts all the form fields in the PHP $_POST object and you can grab them on the process_form.php page (or whatever page you are using for form processing) just as if the form was submitted to that page. For example, $_POST['email']

Upvotes: 1

mcgrailm
mcgrailm

Reputation: 17640

yes !

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

answer to comment

one way would be to map them

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

Upvotes: 2

Dereleased
Dereleased

Reputation: 10087

Probably the easiest way to do it is something like this:

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

Upvotes: 1

zod
zod

Reputation: 12417

.post is a shorthand Ajax function

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

Upvotes: 0

Related Questions