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!!
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!!