title: How to get a string from external PHP file and set it as var in JavaScript
body: <p>I thought this would be simple enough, but I can't get it to work.</p>
  
  <p>I have two files. <code>main.html</code> and <code>data.php</code>
  The two files are in the same folder on a server.</p>
  
  <p>I want to get a string from the <code>PHP</code> file and use it in jQuery. In the example below, I want the browser to create a pop-up, with the text "xxxx". I get no pop-up.</p>
  
  <h2><code>data.php</code></h2>
  
  <pre><code>&lt;?php
  
  $var = "xxxx";
  
  echo json_encode($var);
  
  ?&gt;
  </code></pre>
  
  <h2><code>main.html</code></h2>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;    
      &lt;head&gt;      
          &lt;script type="text/javascript" src="scripts/jquery-1.11.1.js"&gt;&lt;/script&gt;
          &lt;script&gt;
              $(document).ready(function() {
                  var testString = &lt;?php $var = json_decode(file_get_contents('data.php'), true); echo $var; ?&gt;;
                  alert(testString);
              });
          &lt;/script&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;h1&gt;Test&lt;/h1&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Any takers?</p>