pieterv 0 Newbie Poster

I should also mention that you can reach the getPage() function from inside any iframe or object:

<html>
<body>
<a href="" onclick="location.href='javascript: window.parent.getPage(1);'">get me from foo.html to bar.html</a>
</body>
</html>

All this may seem trivial to you, but it wasn't to me a few days ago. In my struggle to get out of the iframe/object, I turned to php. Cheers.

pieterv 0 Newbie Poster

Well, of course I wanted to do something more interesting than printing 'hello world'. My example code above was just a dumbed down version of a more elaborate piece of code which I wanted to write. My eventual aim was to develop a dynamic website in which the user would see certain html pages depending on a number of JavaScript buttons. The filenames would be stored in a Javascript array and then fed into php. But it turns out this is actually quite easy to do with JavaScript alone. In case anyone's interested, here's some code (again simplified for the sake of clarity):

<html>
<script type="text/javascript">
var i=0;
var fnames = ["foo.html","bar.html"];
function getPage(j){
    if (j>=0 && j<fnames.length){
        i=j;
        document.getElementById('test').innerHTML =
             "<object data=\"" + fnames[i] + "\" type=\"text/html\"";
    }
}
</script>
<body>
<div id="test"></div>
<button onclick='getPage(0);'>Foo</button>
</body>
</html>
pieterv 0 Newbie Poster

Okay, but it doesn't matter. Nyight is ryight: there doesn't seem to be an easy way to let JavaScript write PHP code.

Fortunately, I think I just found a way to do what I want to do using JavaScript only. I'll leave PHP alone for the time being. Learned a lot, though. Thanks for all your help!

pieterv 0 Newbie Poster

Final question. If I solve this, then I start coding in earnest. Why does the code in my previous post work, but the following code doesn't?

<html>
<head>
<script type="text/javascript">
      function test(){
           var fname = "./test.txt";
           document.getElementById("php_code").innerHTML = "<?php $escaped_value = str_replace(chr(13),'n',file_get_contents('" + fname + "')); $escaped_value = str_replace(chr(10),'',$escaped_value); echo $escaped_value; ?>";
      }
</script>
</head>
<body>
<a href="#" onclick="test(); return false;"> test </a>
<span id="php_code"></span>
</body>
<html>

The only difference is that the PHP command is made by concatenating three substrings. Here's the result:

<html>
<head>
<script type="text/javascript">
      function test(){
           var fname = "./test.txt";
           document.getElementById("php_code").innerHTML = "";
      }
</script>
</head>
<body>
<a href="#" onclick="test(); return false;"> test </a>
<span id="php_code"></span>
</body>
<html>

Clearly the PHP string following the innerHTML command hasn't been evaluated correctly. Any ideas?

pieterv 0 Newbie Poster

Found a solution that seems to work, using methods taken from this blog: http://tinyurl.com/26gw3y2

<html>
<head>
<script type="text/javascript">
      function test(){
           document.getElementById("php_code").innerHTML = "<?php $escaped_value = str_replace(chr(13),'n',file_get_contents('./test.txt'));             
$escaped_value = str_replace(chr(10),'',$escaped_value); echo $escaped_value ; ?>";
      }
</script>
</head>
<body>
<a href="#" onclick="test(); return false;"> test </a>
<span id="php_code"></span>
</body>
<html>

The added text basically replaces ascii line breaks into Javascript line breaks.

pieterv 0 Newbie Poster

Nyight's suggestion of viewing the html source after clicking the 'test' link is a good one. You can see I'm a newbie at debugging php. Here's the source code. I don't know why there is a carriage return as I didn't put one in 'test.txt'.

<html>
      <head>
      <script type="text/javascript">
      function test(){
      document.getElementById("php_code").innerHTML = "hello world
";
      }
      </script>
      </head>
      <body>
      <a href="#" onclick="test(); return false;"> test </a>
      <span id="php_code"></span>
      </body>
      <html>

I'm running my code in Firefox.

pieterv 0 Newbie Poster

No '/test.txt' doesn't work, and neither does 'test.txt'.

The following code gives the desired output:

<span id="php_code"><?php echo file_get_contents('./test.txt') ; ?></span>

So my php code is correct. But why doesn't it work when called from JavaScript?

pieterv 0 Newbie Poster

Right. Thanks a lot guys. Now that the most primitive 'hello world' version of my intended program works, please have a look at the following, slightly more advanced example. Why does this not work?

<html>
<head>
<script type="text/javascript">
  function test(){
    document.getElementById("php_code").innerHTML = "<?php echo file_get_contents('./test.txt') ; ?>";
  }
</script>
</head>
<body>
<a href="#" onclick="test(); return false;"> test </a>
<span id="php_code"></span>
</body>
<html>

Where the contents of test.txt is simply "hello world" (without quotation marks).

Your help is much appreciated.

pieterv 0 Newbie Poster

Could someone please explain why the following code works:

<html>
<head>
<script type="text/javascript">
        function test(){
         phpTest = "<?php echo \'hello\'; ?>";
         document.getElementById("php_code").innerHTML = phpTest;
        }
</script>
</head>
<body>
<a href="#" onclick="test(); return false;"> test </a>
<span id="php_code"> </span>
</body>
<html>

while this code doesn't:

<html>
<head>
<script type="text/javascript">
        function test(){
         phpTest = "<?php echo \'hello world\'; ?>";
         document.getElementById("php_code").innerHTML = phpTest;
        }
</script>
</head>
<body>
<a href="#" onclick="test(); return false;"> test </a>
<span id="php_code"> </span>
</body>
<html>

(NB: the only difference is, that the first string says 'hello', and the second 'hello world')