need help with AJAX (php and javascript)

J

jhubsta

Hello, I am somewhat of a newbie to web development, but I'm learning
quickly. I am trying to make a slideshow that, given a directory, will
show all the images in that directory.

I can manage to use AJAX to print a string of all the image files on
the client-side page. My problem is that I want the data sent as XML,
which I can parse and manipulate on the client side. I can get this
output from my server script:

*THERES A BLANK LINE HERE*
<?xml version="1.0"?>
<list>
<filename>images/photo_gallery/photo_gallery1.jpg</filename>
<filename>images/photo_gallery/photo_gallery2.jpg</filename>
.......
....
</list>

Server-side, PHP:

dirTreeToArray returns all the files in a directory as an associative
array.
send_data creates a new xml file with all the filenames in it:

function send_data()
{
$dir = 'images/photo_gallery';

$file_list = dirTreeToArray($dir);

$doc = new DOMDocument('1.0');
$doc->formatOutput = true;
$root = $doc->createElement('list');
$root = $doc->appendChild($root);

foreach($file_list as $img_name) {
$img = $doc->createElement('filename');
$img = $root->appendChild($img);

$child = $doc->createTextNode($dir."/".$img_name);
$child = $img->appendChild($child);
}

$output = $doc->saveXML();

echo $output;
}


Client-side, Javascript:
.....
var x = (window.ActiveXObject) ? new
ActiveXObject("Microsoft.XMLHTTP") :
new XMLHttpRequest();

if (x) {
x.onreadystatechange = function () {
if(x.readyState == 4 && x.status == 200) {

el = document.getElementById(div_element_name);
el.innerHTML = x.responseText; }}}


x.open("GET",url,true);
x.send(null);}

If I use x.responseXML, it says that there is no xml declaration at the
beginning of the page, I think this is due to the fact that the output
from my php script leaves 1 blank line at the very beginning. If is use
trim($output) it does not fix the problem.

In short, my main issue is, how do I format and send the data on the
server-side, so that it may be parsed and treated as a DOMDocument on
the client side??

Thank you!!
 
C

chonny

........
$child = $doc->createTextNode($dir."/".$img_name);
$child = $img->appendChild($child);
}

$output = $doc->saveXML();

header('Content-Type: text/xml');

echo $output;
}

hope this help
 
M

Moot

In short, my main issue is, how do I format and send the data on the
server-side, so that it may be parsed and treated as a DOMDocument on
the client side??

Thank you!!

Are you setting?:
header("Content-type: text/xml");

I believe even if you send xml formatted text to the browser, you still
must set the content type for it to recognize it properly.
 
J

jhubsta

$output = $doc->saveXML();
header('Content-Type: text/xml');

echo $output;
I added the header line and it made some progress. Now I get this
error:

XML Parsing Error: xml declaration not at start of external entity
Location:.......
Line Number 2, Column 1:
<?xml version="1.0"?>
^

I've heard that this can happen if there's some whitespace before the
xml declaration... for some reason I keep getting one blank line above
the declaration. Is there something I'm doing wrong, or will I just
have to use substr($output...) or something?
Thanks!
 
C

chonny

check your xml output for synatax errors


(e-mail address removed) напиÑа:
 
J

jhubsta

using trim($output) and rtrim($output, '\n') produced no effect, but I
just discovered that the script works with IE, but not with Firefox.

Is there some subtlety to Firefox that I am missing??

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top