1

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?

1
  • The code you are showing is javascript, not PHP. Elaborate on your code (with more of the JS or PHP) and question please. Commented May 31, 2012 at 17:12

2 Answers 2

1

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

10 Comments

Like this: <?php $titolari = $_GET['titolari']; for ($i = 1; $i <= 300; $i++) { echo $titolari[$i]; } ?> It doesn't echo anything...
Tommaso, my apologies! A bit down in the documentation I found this: "The POST method is used if data is provided as an object". I have updated my answer.
I'd like to specify that jQuery load formazione-realtime.inc.php, and this php page echos in the same page of jQuery. I don't know if it's a problem for _POST. And also titolari is an Array.
Then I'm afraid I know too little about jQuery to help you here. Maybe you can provide a full test case in your question?
just for test, this brings anything home? $.post("formazione-realtime.inc.php",{"titolari" : titolari})
|
0

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;

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.