good_evening
good_evening

Reputation: 21739

Pass array in jQuery ajax

My imaginery code:

$(document).ready(function() {
    $("#sub").click(function() {
        info['moto']  = $("#moto").val();
        info['motox'] = $("#motox").val();

        $.ajax({
            type: "POST",
            url: "index.php",
            data: "arr="+info,
            success: function(msg){
                $('.answer').html(msg);
            }
        })
    })
})

How could I make, that after receiving it in .php file I could use POST method like this: $_POST['moto'] and $_POST['motox'] or something like that? What should I change? Thanks.

Upvotes: 2

Views: 10731

Answers (3)

No Results Found
No Results Found

Reputation: 102735

Check out jQuery serialize(), it does all the work for you if you are working with form inputs.

Upvotes: 1

Quentin
Quentin

Reputation: 943152

Just:

data: info,

(And you need to initialize info as an object in the first place: var info = {})

Upvotes: 10

Reporter
Reporter

Reputation: 3948

I thinkyoushould go also the same way like jquery-ajax-data-array-from-php

Upvotes: 0

Related Questions