tags:

views:

34

answers:

2

I was following this tutorial.

I need to use a php file's ouput in my HTML file to dynamically load images into a gallery. I call

    function setOutput()
    {
     if (httpObject.readyState == 4)
      document.getElementById('main').src = httpObject.responseText;
      alert("set output: " + httpObject.responseText);
    }

from

    function doWork() 
    {
     httpObject = getHTTPObject();
     if (httpObject != null) {
      httpObject.open("GET", "gallery.php?no=0", true);
      httpObject.send(null);
      httpObject.onreadystatechange = setOutput;
     }
    }

However, the alert returns the php file, word for word. It's probably a really stupid error, but I can't seem to find it.

The php file:

<?php

    if (isset($_GET['no'])) {
     $no = $_GET['no'];
     if ($no <= 10 && $no >1) {
      $xml = simplexml_load_file('gallery.xml');
      echo "images/" . $xml->image[$no]->src;
     }
     else die("Number isn't between 1 and 10");
    }
    else die("No number set.");

?>
+3  A: 

If the alert is returning the contents of the PHP file instead of the results of executing it, then the server is not executing it.

Test by accessing the URI directly (instead of going via JavaScript).

You probably need to configure PHP support on the server.

David Dorward
Outputting a file directly from PHP would need the right header too, in case of a image, something like: header('Content-type: image/jpg'); The avaiable content-types are here: http://it.wikipedia.org/wiki/Wikipedia:Elenchi_generati_offline/Collegamenti_esterni/Content-type
DaNieL
Thanks, just had to configure the server.
pypmannetjies
A: 

Your Server doesn't serve/parse PHP files! You could test your JavaScript code by setting the content of gallery.php to the HTML code you want to receive.

powtac