I am generating an xml using a xml.php page as follows
<?php
$path = trim(ReadData("data_file_path"));
$speed_data_file = $path . "speed";
$rmp_data_file = $path . "rpm";
$temp_data_file = $path . "temp";
$fuel_data_file = $path . "fuel";
header("Content-type: text/xml");
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
echo '<table_info>';
echo "<speed>";
echo ReadData($speed_data_file);
echo "</speed>";
echo "<rpm>";
echo ReadData($rmp_data_file);
echo "</rpm>";
echo "<temp>";
echo ReadData($temp_data_file);
echo "</temp>";
echo "<fuel>";
echo ReadData($fuel_data_file);
echo "</fuel>";
echo '</table_info>';
function ReadData($filepath)
{
$data = file_get_contents($filepath) or null;
return $data;
}
?>
Under my main.php page I am reading the above xml content under onload event of the body tag.
...
function Onload()
{
var xml_data_file = "http://" + "localhost" + "/demo/xml.php";
loadXMLDoc(xml_data_file, function() {updateXMLtoElement()});
}
function loadXMLDoc(url,cfunc)
{
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else {
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=cfunc;
xmlhttp.open("GET",url,true);
xmlhttp.send();
}
function updateXMLtoElement()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
z = xmlhttp.responseXML;
if(z.getElementsByTagName("speed")[0].childNodes[0].nodeValue != null) {
document.getElementById("speed_dial").setAttribute("data-value", z.getElementsByTagName("speed")[0].childNodes[0].nodeValue);
}
if(z.getElementsByTagName("rpm")[0].childNodes[0].nodeValue != null) {
document.getElementById("rmp_dial").setAttribute("data-value", z.getElementsByTagName("rpm")[0].childNodes[0].nodeValue);
}
if(z.getElementsByTagName("temp")[0].childNodes[0].nodeValue != null) {
document.getElementById("temp_dial").setAttribute("data-value", z.getElementsByTagName("temp")[0].childNodes[0].nodeValue);
}
if(z.getElementsByTagName("fuel")[0].childNodes[0].nodeValue != null) {
document.getElementById("fuel_dial").setAttribute("data-value", z.getElementsByTagName("fuel")[0].childNodes[0].nodeValue);
}
}
}
...
Its all working fine, but when I change Onload inthe above code as show below It stops working.
function Onload()
{
var xml_data_file = "http://" + "<?php echo $_SERVER['SERVER_ADDR'].":".$_SERVER['SERVER_PORT']; ?>" + "/demo/xml.php";
loadXMLDoc(xml_data_file, function() {updateXMLtoElement()});
}
Using "inspect element" functionality of chrome it seems that both attributes $_SERVER['SERVER_ADDR'] and $_SERVER['SERVER_PORT'] resply, are responding with proper server address and port. So what's going wrong ?