9

i have this ajax code

xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange=function(){
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
        document.getElementById('addIO').innerHTML+=xmlhttp.responseText;
    }
}
xmlhttp.open("GET","http://localhost/Mar7ba/Ontology/getRelatedConceptsAndRelations/3/TRUE",true);
xmlhttp.send();

and I have a php array

$cars=array("Saab","Volvo","BMW","Toyota");

how can i send the array $cars to my javascript ?

William Kinaan
  • 28,059
  • 20
  • 85
  • 118
  • possible duplicate of [How to pass an array of strings from PHP to Javascript using $.ajax()?](http://stackoverflow.com/questions/3499757/how-to-pass-an-array-of-strings-from-php-to-javascript-using-ajax) – Felix Kling May 06 '12 at 21:33
  • Also see [how to parse json in javascript](http://stackoverflow.com/questions/4935632/how-to-parse-json-in-javascript). – Felix Kling May 06 '12 at 21:36
  • i am trying to read it , thank you – William Kinaan May 06 '12 at 22:03

2 Answers2

16

PHP

echo json_encode($cars);

JavaScript

Native:

var foo = JSON.parse(xmlhttp.responseText);

With jQuery:

var foo = $.parseJSON(xmlhttp.responseText);
//or
$.getJSON("url", function(data){
    //data is your array
});

UPDATE

if(xmlhttp.readyState==4 && xmlhttp.status==200){
     //document.getElementById('addIO').innerHTML+=xmlhttp.responseText;
    var cars = JSON.parse(xmlhttp.responseText);  //cars will now be the array.
     //Do whatever you want here.
    $("#addIO").html(cars.join(", "));     //Join array with ", " then put it in addIO
}

If you want to use jQuery, put this in <head>:

<script type="text/javascript" src="link/to/the/file/jquery.js"></script>
Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
  • It works, and if your browser does not support it, try [jQuery](http://api.jquery.com/jQuery.parseJSON/). – Derek 朕會功夫 May 06 '12 at 21:18
  • do u mean that i have to do ur code instead of my fourth code in my function? – William Kinaan May 06 '12 at 21:23
  • i don't know jQuery, so please would u tell me where should i put ur javascript line code in my function – William Kinaan May 06 '12 at 21:28
  • i tried this on my function document.getElementById('addIO').innerHTML=foo[0]; but it doesn't echo anythink – William Kinaan May 06 '12 at 21:34
  • 1
    @William: This has nothing to do with jQuery. `xmlhttp.responseText` contains the response of the request as you already know. Put the code there where you have access to `xmlhttp.responseText`. If you are new to Ajax, you might want to read https://developer.mozilla.org/en/AJAX/Getting_Started – Felix Kling May 06 '12 at 21:34
  • @WilliamKinaan jQuery is only used for parsing JSON if the browser does not support it. – Derek 朕會功夫 May 06 '12 at 21:36
  • thank you all i am using google chrome is my last two lines should still there ? – William Kinaan May 06 '12 at 21:46
  • when i put the update section and after it i tried to alert("roma") , even alert doesn't work , :( – William Kinaan May 06 '12 at 22:01
  • it wooooooooooooooooooooooooooooooooooooooooooooooooooooooorks thaaaaaaaaaaaaaaaaaaaank youuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu i loooooooooooooooooove youuuuuuuuuuuuuuuuu:) :) – William Kinaan May 06 '12 at 22:06
5

Use JSON:

echo json_encode($cars);
Marc B
  • 356,200
  • 43
  • 426
  • 500