Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass array in jQuery ajax

Tags:

jquery

php

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.

like image 524
good_evening Avatar asked Feb 17 '26 11:02

good_evening


1 Answers

Just:

data: info,

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

like image 91
Quentin Avatar answered Feb 19 '26 01:02

Quentin