title: How to use JSON from db/php in JavaScript
body: <p>I'm using a map plugin to render some data. The data comes from the DB and into a json file - the script works great. I decided to use the data directly from the php output instead of making a json file. For some reason the javaScript doesn't accept the direct php input. I'm using codeigniter MVC</p>
  
  <p>Here is the sample code that currently works:</p>
  
  <pre><code>$.getJSON('_data/index/data.json', function(data){ ...
  </code></pre>
  
  <p>Here is what I tried:</p>
  
  <pre><code> var dataMap = '&lt;? print $mapData;?&gt;';
          $.getJSON(dataMap, function(data){...
  </code></pre>
  
  <p><strong>* EDIT 2 *</strong></p>
  
  <p>Based on the answers - this option doesn't work either.</p>
  
  <pre><code>var dataMap = '&lt;?php echo $mapData;?&gt;';
          $.get(dataMap, function(data){...
  </code></pre>
  
  <p>And here is the json data</p>
  
  <pre><code>{"countries":{"AL":"1","GB":"1","RS":"1","BG":"6","CA":"3","AT":"2","CD":"1"}}
  </code></pre>
  
  <p><em><strong>EDIT</em></strong></p>
  
  <p>$mapData is </p>
  
  <pre><code>FOREACH LOOP
   $retdata['countries'][] = strtoupper($row-&gt;code);
   $retdata['num'][] = $row-&gt;num;
  ENDFOREACH LOOP
   $retdata['countries'] = array_combine($retdat['code'], $retdata['num']);
   $retdata = json_encode($retdata);
  </code></pre>
  
  <p>And then it is printed into a file as normal. This is into the model, then I pass it to the controller and then into the view. The string is the same in the <code>$dataMap</code> that I have on file and the one that is being sent to the view.</p>