Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i have 4 strings in my application like this that i want to pass to my js file

$a1='[10,20,13,14]';
$a2='[17,15,14,16]';
$a3='[18,24,16,17]';
$a4='[15,54,18,27]';

echo $a1.",".$a2.",".$a3.",".$a4;

and my javascriptcode is

$.ajax({
           type: "POST",
           dataType: "json",
           url: "loaddata.php",
               success: function(data)
            {
           alert(data); //alert 15,54,18,27


          }
     });

i can get just $a4 string, and i can not get other string

how can i pass these 4 strings in php and set these 4 variables in javascript

thanks;

share|improve this question
add comment

5 Answers

up vote 5 down vote accepted

Encode them as JSON.

On the PHP side:

echo json_encode(array("a1" => $a1, "a2" => $a2, "a3" => $a3, "a4" => $a4));

On the JavaScript side:

$.ajax({
    type: "POST",
    dataType: "json",
    url: "loaddata.php",
    success: function(data) {
        var a1=data.a1;
        var a2=data.a2;
        var a3=data.a3;
        var a4=data.a4;
        // do something with a1, a2, a3 and a4
    }
});

And if you want a1, a2, a3, and a4 to be arrays of numbers instead of strings containing numbers, just JSON decode the strings on the PHP side before sending them over:

echo json_encode(array(
    "a1" => json_decode($a1),
    "a2" => json_decode($a2),
    "a3" => json_decode($a3),
    "a4" => json_decode($a4)
));
share|improve this answer
 
I don't think it's neccesary to nest the json_encode's. A wrapper json_encode jsonEncodes all the object, doesn't? –  Erik Escobedo Jul 7 '10 at 4:10
 
@Erik: Yes, but see, I'm not nesting json_encodes. It's decoding them, then re-encoding the complete structure. –  icktoofay Jul 9 '10 at 0:38
add comment

Use json_encode on the php side and pass the data back that way http://us3.php.net/manual/en/function.json-encode.php

share|improve this answer
add comment

Use the serialize() method. Pass the variables in a input field and:

$.post("loaddata.php", $("#ajaxform").serialize(), function(data){
  //
});
share|improve this answer
 
I think you misread the question. –  Felix Kling Jul 4 '10 at 8:28
add comment
$a1='[10,20,13,14]';
$a2='[17,15,14,16]';
$a3='[18,24,16,17]';
$a4='[15,54,18,27]';

echo $a1.",".$a2.",".$a3.",".$a4;

will return

[10,20,13,14],[17,15,14,16],[18,24,16,17],[15,54,18,27]

this is not a valid JSON string.

You can return an object that contains an array of arrays like this:

echo "[".$a1.",".$a2.",".$a3.",".$a4."]";
// => [[10,20,13,14],[17,15,14,16],[18,24,16,17],[15,54,18,27]]

or you can do return an object with named keys to access the different arrays.

echo "{a1:".$a1.",a2:".$a2.",a3:".$a3",a4:".$a4."};
// => {a1:[10,20,13,14], a2:[17,15,14,16], a3:[18,24,16,17], a4:[15,54,18,27]}

Or even much butter: Don't build the JSON String on your own and use PHP's json_encode instead:

$a1=array(10,20,13,14);
$a2=array(17,15,14,16);
$a3=array(18,24,16,17);
$a4=array(15,54,18,27);

echo json_encode(array($a1,$a2,$a3,$a4));
// => [[10,20,13,14],[17,15,14,16],[18,24,16,17],[15,54,18,27]]
// or if you want an associative array
echo json_encode(array("a1" => $a1, "a2" => $a2, "a3"=> $a3, "a4" => $a4));
// => {a1:[10,20,13,14], a2:[17,15,14,16], a3:[18,24,16,17], a4:[15,54,18,27]}
share|improve this answer
add comment
return "{ A1: '[10,20,13,14]', A2: '[17,15,14,16]', A3: '[18,24,16,17]', A4: '[15,54,18,27]' }";

Though you could also just pass these as actual arrays by removing the quotes.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.