简体   繁体   English

PHP-回显Javascript函数

[英]PHP - Echoing Javascript functions

I have seen other posts about echoing JS code, but it's not working for my JS code. 我看过其他有关回显JS代码的帖子,但是不适用于我的JS代码。 I don't know if its because I'm echoing AJAX calls too, but I can't see why PHP would fuss about that. 我不知道这是否是因为我也回响了AJAX调用,但是我不明白为什么PHP对此大惊小怪。

What have I done wrong in echoing transforming these JS calls into its equivalent PHP calls? 在将这些JS调用转换为等效的PHP调用时,我做错了什么?

JavaScript code: JavaScript代码:

    <script language="Javascript">
    var countdown;
    var i=0;
    countdown = setInterval(function(){
        var xmlhttp;
        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=function(){
            if (xmlhttp.readyState==4 && xmlhttp.status==200){
                var JSONobj=JSON.parse(xmlhttp.responseText);
                document.getElementById("homelink").innerHTML=i;
                i++;
            }
        }
        xmlhttp.open("GET","updateindex.php",true);
        xmlhttp.send();
    },3000);
    </script>

PHP echo's for the above JavaScript code(what I need, but is not working): 上面的JavaScript代码的PHP echo(我需要,但不起作用):

    <?php
    echo "<script language='Javascript'>";
    echo "var countdown;";
    echo "var i=0;";
    echo "countdown = setInterval(function(){";
    echo "var xmlhttp;";
    echo "if (window.XMLHttpRequest){";
    echo "// code for IE7+, Firefox, Chrome, Opera, Safari";
    echo "xmlhttp=new XMLHttpRequest();";
    echo "}else{";
    echo "// code for IE6, IE5";
    echo "xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');";
    echo "}";
    echo "xmlhttp.onreadystatechange=function(){";
    echo "var JSONobj=JSON.parse(xmlhttp.responseText);";
    echo "document.getElementById('homelink').innerHTML=i;";
    echo "i++;";
    echo "}";
    echo "}";
    echo "xmlhttp.open('GET','updateindex.php',true);";
    echo "xmlhttp.send();";
    echo "},3000);";
    echo "</script>";
    ?>

My PHP code attempt above simply does nothing. 我上面的PHP代码尝试无济于事。

You are not echoing newline ( \\n ) characters, so the entire output is one line of text. 您没有回显换行符( \\n ),因此整个输出是一行文本。

In order for that to work, you need to be perfect in your JS syntax use of semi-colons, curly braces, etc. Furthermore, you are using single line comments ( // ) in this one resulting line of output. 为了使其正常工作,您需要在JS语法中完美使用分号,花括号等。此外,在此输出结果行中使用单行注释( // )。 Once the first of those is hit by the parser, the rest of the line (which is the rest of your code) is a comment. 一旦第一个被解析器击中,该行的其余部分(即代码的其余部分)即为注释。 Multi-line comment notation ( /* comment */ ) would be preferred. 最好使用多行注释符号( /* comment */ )。

You need to add newline chars to the end of each echo'd line or use a heredoc or similar long-form string. 您需要在每个回显行的末尾添加换行符,或使用heredoc或类似的长格式字符串。

All that said, echoing JS like this is really bad practice. 综上所述,像这样回显JS确实是一种不好的做法。 You should stop doing that. 您应该停止这样做。 Closing PHP and reopening it when needed is good start. 关闭PHP并在需要时重新打开它是一个好的开始。 Moving the bulk of your JS to external JS files is better. 将大部分JS移至外部JS文件会更好。 If you need PHP to output data to JS, there are many other ways to accomplish it. 如果您需要PHP将数据输出到JS,还有许多其他方法可以实现它。

Addition from comments: You also have an extra echo "}"; 注释的补充:您还具有一个额外的echo "}"; line right before your echo "xmlhttp.open line. This sort of thing is common when trying to echo one lang from another, so it's one of the reasons I say you should stop. 在您的echo "xmlhttp.open行之前。”这行在尝试从另一个lang回显一个lang时很常见,所以这是我说您应该停止的原因之一。

Also, in your check for onreadystatechange you're not checking that the request had completed and was successful before you try to parse the response. 另外,在检查onreadystatechange您没有在尝试解析响应之前检查请求是否已完成并且是否成功。

There may be other things wrong as well, but that's what I have so far. 可能还有其他错误,但这就是我到目前为止的问题。

Try closing and reopening PHP tags: 尝试关闭并重新打开PHP标签:

?>
<script language="Javascript">
var countdown;
var i=0;
countdown = setInterval(function(){
    var xmlhttp;
    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=function(){
        if (xmlhttp.readyState==4 && xmlhttp.status==200){
            var JSONobj=JSON.parse(xmlhttp.responseText);
            document.getElementById("homelink").innerHTML=i;
            i++;
        }
    }
    xmlhttp.open("GET","updateindex.php",true);
    xmlhttp.send();
},3000);
</script>
<?php

This also gives you the luxury of highlighted code in the more simple IDEs/editors. 这也使您可以在更简单的IDE /编辑器中使用突出显示的代码。

First, try to echo it this way: 首先,尝试以这种方式回显它:

<?php
echo '<script language="text/javascript">
    var countdown;
    var i=0;
    countdown = setInterval(function(){
        var xmlhttp;
        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=function(){
            if (xmlhttp.readyState==4 && xmlhttp.status==200){
                var JSONobj=JSON.parse(xmlhttp.responseText);
                document.getElementById("homelink").innerHTML=i;
                i++;
            }
        }
        xmlhttp.open("GET","updateindex.php",true);
        xmlhttp.send();
    },3000);
    </script>';
?>

Check your page source if it really doesn't echo. 检查您的页面来源是否真的没有回声。

I just wanna add that it's pretty bad practice, but we're just trying to figure out why it doesn't work, not how to do it the best way... 我只想补充说,这是非常糟糕的做法,但我们只是想弄清楚为什么它不起作用,而不是如何以最佳方式做到这一点...

You could make it easier for yourself by only echoing a function name. 您可以通过仅回显函数名称来使自己更轻松。 You could then reuse the code elsewhere. 然后,您可以在其他地方重用代码。 It would look something like this: 它看起来像这样:

Javascript (in an external file maybe): Javascript(可能在外部文件中):

function someFunction(methode,url){
    var countdown;
    var i=0;
    countdown = setInterval(function(){
        var xmlhttp;
        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=function(){
            if (xmlhttp.readyState==4 && xmlhttp.status==200){
                var JSONobj=JSON.parse(xmlhttp.responseText);
                document.getElementById("homelink").innerHTML=i;
                i++;
            }
        }
        xmlhttp.open(methode,url,true);
        xmlhttp.send();
    },3000);
}

PHP: PHP:

<?PHP echo '<script language="text/javascript"> someFunction("GET","updateindex.php"); </script>' ?>

Sidenote 边注

The Chrome console and Firebug for Firefox are excellent tools for javascript debugging. Chrome控制台和适用于Firefox的Firebug是用于JavaScript调试的出色工具。

You not only echo the script, you have to invoke the script: 您不仅要回显脚本,还必须调用脚本:

  echo '<script> (function(){ var countdown; var i=0; countdown = setInterval(function(){ var xmlhttp; 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=function(){ if (xmlhttp.readyState==4 && xmlhttp.status==200){ var JSONobj=JSON.parse(xmlhttp.responseText); document.getElementById("homelink").innerHTML=i; i++; } } xmlhttp.open("GET","updateindex.php",true); xmlhttp.send(); },3000); })(); </script>'; 


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM