title: Trying to get json_encode array from PHP with AJAX into a Javascript function
body: <p>In my PHP file, I create an array of arrays, and use json_encode to make this array usable in javascript. When I echo this direction within the  tags it works perfectly. However, I need the PHP to be run at regular intervals (I'm using setTimeout), so I'm trying to use AJAX to call the php and get that array to be used in the javascript function.</p>
  
  <p>Here is the PHP:</p>
  
  <pre><code>$bubbles = array();
  $result = mysql_query("SELECT text, lat, lng FROM bubbles");
  while($row = mysql_fetch_array($result)){
  $newbubble = array($row[0],$row[1],$row[2],'female-2.png');
  $bubbles[] = $newbubble;
  }
  $js_array =  json_encode($bubbles);
  echo"$js_array";
  </code></pre>
  
  <p>And here is the javascript/AJAX portion in question:</p>
  
  <pre><code>setTimeout(initializeMaps, 5000);
  
  function initializeMaps() {
  var markers;
  var ajax;
  ajax=new XMLHttpRequest();
  ajax.onreadystatechange=function()
    {
    if (ajax.readyState==4 &amp;&amp; ajax.status==200)
      {
  var markers = ajax.responseText;
      }
    }
  ajax.open("GET","ajax-getbubbles.php",true);
  ajax.send();
  
  
  /*The markers variable needs to be used in the following code*/
  var iconBase = 'http://picaflora.com/uniproject/images/';
  var infowindow = new google.maps.InfoWindow(), marker, i;
  for (i = 0; i &lt; markers.length; i++) {  
      marker = new google.maps.Marker({
          position: new google.maps.LatLng(markers[i][1], markers[i][2]),
          map: map,
          icon: iconBase + markers[i][3]
      });
      google.maps.event.addListener(marker, 'click', (function(marker, i) {
          return function() {
              infowindow.setContent(markers[i][0]);
              infowindow.open(map, marker);
          }
      })(marker, i));
  }
  setTimeout(initializeMaps, 5000);
  }
  </code></pre>
  
  <p>If I alert() or console.log(), all the data is coming back from PHP just fine. But somehow it seems that the rest of the script just isn't doing anything with the variable markers. I'm not too familiar with javascript, just familiar enough to tinker and try things till it works, so if you think there might be a better way to approach the problem, then by all means go for it. I'd rather not look into jQuery solutions at this point, unless it's necessary. Thanks!</p>