dongxun3777 2016-12-12 10:43
浏览 36
已采纳

PHP SERVER属性在javascript下的函数中不起作用?

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 ?

  • 写回答

1条回答 默认 最新

  • douqi2571 2016-12-12 11:07
    关注

    Usually Javascript files are not processed by PHP. There are very solid reasons, which are out of scope. The <?php... strings are used in javascript as is, not as the values you expect there.

    What you are trying to achieve should be done this way:

    function Onload() 
    {    
        var xml_data_file = "/demo/xml.php";
        loadXMLDoc(xml_data_file, function() {updateXMLtoElement()});
    }
    

    Which will use schema, domain and port of current page: "http://localhost/demo/xml.php" when it is opened on http://localhost, and "http://example.com/demo/xml.php" when it is opened on http://example.com.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题