title: Generating JavaScript with PHP
body: <p>I found a tutorial on  PHP.net in the manual "PHP and HTML"  and there is an example, <a href="http://www.php.net/manual/en/faq.html.php#example-5473" rel="nofollow">Generating JavaScript with PHP</a>.</p>
  
  <p>I am trying out a simple demo version with this on my own to learn how to do this so I can later attempt something more complex. Right now, I'm simply trying to declare a string variable in PHP (an address to a JPG file) and then through JavaScript (created in the PHP script) change the src of an IMG element to this new address. </p>
  
  <p>Someone suggested something with JSON, which I have a little experience with, but only with posting to textfile using script in a PHP file. I am not sure if I can use a GET request or something, I honestly have no clue. I just didn't think this would be that complicated. </p>
  
  <p>Here is the link to <a href="http://tinyurl.com/kd3gynk" rel="nofollow">my page</a> where I am trying to do this.</p>
  
  <p>As you see, I've actually been trying to do the opposite of creating the JavaScript in PHP, instead I was trying to embed the PHP within the JavaScript, which is what someone originally suggested to me, which didn't work. So that is why it is like that. </p>
  
  <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;
  &lt;title&gt;Demo&lt;/title&gt;
  
      &lt;?php
  
  
  
      $srcmsg = 'http://www.newyorker.com/online/blogs/photobooth/NASAEarth-01.jpg';
      ?&gt;
      &lt;script type="text/javascript"&gt;
      //&lt;![CDATA[
      //
  
      var msr = "&lt;?php echo $srcmsg; ?&gt;";
  
      window.onload = document.getElementsByTagName('img').src= msr;
      //]]&gt;
      &lt;/script&gt;
      &lt;/head&gt;
      &lt;body&gt;&lt;img src="#" alt="Picture of the world" height="42" width="42" /&gt;
      &lt;/body&gt;
      &lt;/html&gt;
  </code></pre>
  
  <p><strong>SOLUTION:</strong> this was discovered by Orangepill and Fred....
  it turns out that one of the big problems was the way my server was not able to parse the script in the html file so I had to put it in a PHP file instead. then there was an issue with interpreting the short_open tags in the xml declaration. so here is how it ended up for it to get working: keep in mind this is a .php file NOT .htm </p>
  
  <pre><code>&lt;?php echo "&lt;", 'xml version="1.0" encoding="UTF-8" standalone="no" ?'; "&gt;\n"; ?&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;
  &lt;title&gt;Demo&lt;/title&gt;
  &lt;script type="text/javascript"&gt;
  //&lt;![CDATA[
  //
  
  window.onload = function (){
  var msr = '&lt;?php $srcmsg = "http://www.newyorker.com/online/blogs/photobooth/NASAEarth-01.jpg"; echo $srcmsg; ?&gt;';
  
  var x = document.getElementsByTagName('img')[0];
  
  x.src = msr;
  }
  //]]&gt;
  &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;&lt;img src="#" alt="Picture of the world" height="42" width="42" /&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>