Tom
Tom

Reputation: 11

How to .load to a php page an array?

How could I sent to a php page the array titolari?

var titolari = $('#titolari').sortable('toArray');
$("#formazione-realtime").load('formazione-realtime.inc.php', {
    "$titolari[]" : titolari
});

The php page is loaded correctly, but the php page seems not to receive the array... how do I take it into my php script?

Upvotes: 1

Views: 103

Answers (2)

Nick Caballero
Nick Caballero

Reputation: 944

Javascript

$.ajax({
    url:'formazione-realtime.inc.php',
    data:{
        'titolari':['ti','to','la','ri']
    },
    context:{},
    type:'POST',
    contentType:'json',
    success:function(data) {
        console.log(data);
        $("#formazione-realtime").html(data);
    },
    error:function() {
        //Handle this
    }
};

PHP

try{
    $vars = json_decode(file_get_contents('php://input'), 1);
catch(Exception $e) {
    //Handle this
}
print_r($vars);
exit;

Upvotes: 0

Emil Vikström
Emil Vikström

Reputation: 91922

As you can see in the jQuery documentation, the call will use the POST method if data is added as an object.

On the client:

var titolari = $('#titolari').sortable('toArray');
$("#formazione-realtime").load('formazione-realtime.inc.php', {
    "titolari" : titolari
});

On the server:

$titolari = $_POST['titolari'];

Upvotes: 1

Related Questions