2

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,

4 Answers 4

2

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 });
Sign up to request clarification or add additional context in comments.

1 Comment

how can I dynamically grab all of the values and place them there, am just a bit confused.
1

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

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

Comments

1

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']

Comments

0

.post is a shorthand Ajax function

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.