I've got a PHP file with a SELECT statement in it that pulls data from a MySQL database. It then processes the data and writes the results to an XML file.
I've got a javascript file that uses the XML file. How do I call a javascript function, for example, doStuff(), after the PHP file has downloaded the data and written the XML file? I could call it after a setTimeout(), but I hope that there is a better way.
The XML file gets over-written each time the PHP script runs. So, the XML file may already exist.
Is there some event that I could have doStuff() fire after? Or a way of determining whether the PHP script has finished?
Any suggestions?
Thank you.
UPDATE:
Here's some more details on what I've got.
I've got a map with a form. When the users submits the form, I use Ajax to process the form. A PHP function is called and data from a MySQL database is selected. The data includes latitude and longitudes. The PHP file then writes an XML file with this data. The javascript file then reads the XML file and places markers on the map.
The PHP and the javascript work fine. The problem is that the javascript file fires immediately, as soon as the map loads. So, the data in the xml file (from a previous form selection) gets turned into markers. If I delete the xml file, then no markers are put on the map. The javascript tried to use the non-existent xml file as soon as it loads. It's not there, so no markers are placed on the map. And this is before the user has made any selections in the form.
I need some way to have the javascript function that loads the XML file do so only after the XML file has been written.
I'm follwing this tutorial . I've had to adapt it in order for it to work in a WordPress plugin.
In my PHP, I've got:
//Creates XML
//each marker has a lat and long
$my_xml ='<?xml version="1.0" ?>';
$my_xml .= '<markers>';
foreach($csf_data as $incident) {
$my_xml .='<marker lat="'. $incident->latitude.'" lng="'.$incident->longitude.'" type="resto" />';
}
$my_xml .= '</markers>';
//Creates the XML file
//(I've hardcoded markers.xml's path for now-- will change later)
$file_handle = fopen('/Users/myname/Sites/mysite/wp-content/plugins/myplugin/markers.xml', 'w');
fwrite($file_handle, $my_xml);
fclose($file_handle);
In the javascript, I've got:
function downloadUrl(url,callback) {
var request = window.ActiveXObject ?
new ActiveXObject('Microsoft.XMLHTTP') :
new XMLHttpRequest;
request.onreadystatechange = function() {
if (request.readyState == 4) {
request.onreadystatechange = doNothing;
callback(request, request.status);
}
};
request.open('GET', url, true);
request.send(null);
}
var customIcons = {
resto: {
icon: 'http://labs.google.com/ridefinder/images/mm_20_blue.png',
shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
},
bar: {
icon: 'http://labs.google.com/ridefinder/images/mm_20_red.png',
shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
}
};
downloadUrl("http://mysite/wp-content/plugins/myplugin/markers.xml", function(data) {
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
var type = markers[i].getAttribute("type");
var point = new google.maps.LatLng(
parseFloat(markers[i].getAttribute("lat")),
parseFloat(markers[i].getAttribute("lng")));
var icon = customIcons[type];
var marker = new google.maps.Marker({
map: map,
position: point,
icon: icon.icon,
shadow: icon.shadow
});
}});
function doNothing() {}
This code works, but I need these javascript functions to run only after PHP has written a new XML file. How can I call this javascript after the new XML file has been written?
Thank you.