简体   繁体   English

回显JavaScript中的PHP变量

[英]PHP Variable in Echoing JavaScript

I have an php script that is echoing JavaScript, in the JavaScript there are PHP variables 我有一个呼应JavaScript的php脚本,在JavaScript中有PHP变量

echo '<script type="text/javascript"> 
     FB.init({
    appId: "myid",
    channelUrl: "//mysite/channel.html",
    status: true, // check login status
    cookie: true, // enable cookies to allow the server to access the session
    xfbml: true // parse XFBML
});
FB.getLoginStatus(function (response) {
    if (response.status === "connected") {
        FB.api("me/bookvote:download", "post", {
            book: "<?php echo "http://mysite/auction_details.php?name=$item_details["name"]&auction_id=$item_details["auction_id"]";?>",
            fb:explicitly_shared = "true" //? Is syntactically valid but creates a global
        }, //Missing a , here?

However, I am still getting: 但是,我仍然得到:

Uncaught SyntaxError: Unexpected identifier for book: http://mysite.xom/auction_details.php?name=$item_details["name"]&auction_id=$item_details["auction_id"]";?>",

What should I do? 我该怎么办?

I can spot multiple issues: 我可以发现多个问题:

  • You try to print variables inside of single quotes. 您尝试在单引号内打印变量。 They won't be parsed, you need double quotes, or better yet, HEREDOC, or even better yet , don't use echo to print HTML/JavaScript. 它们不会被解析,您需要双引号,或者更好,HEREDOC, 甚至更好 ,不要使用echo来打印HTML / JavaScript。
  • You try to use additional <?php ?> tags inside of your echo. 您尝试在回显中使用其他<?php ?>标记。 That would obviously not work. 那显然是行不通的。

Try this. 尝试这个。 I have removed the echo. 我已经删除了回声。 Note that the larger PHP tag ends there. 请注意,较大的PHP标记到此结束。

?>
<script type="text/javascript"> 
     FB.init({
    appId: "myid",
    channelUrl: "//mysite/channel.html",
    status: true, // check login status
    cookie: true, // enable cookies to allow the server to access the session
    xfbml: true // parse XFBML
});
FB.getLoginStatus(function (response) {
    if (response.status === "connected") {
        FB.api("me/bookvote:download", "post", {
            book: "<?php echo "http://mysite/auction_details.php?name=$item_details["name"]&auction_id=$item_details["auction_id"]";?>",
            fb:explicitly_shared = "true" //? Is syntactically valid but creates a global
        }, //Missing a , here?

更改此行:

book: "http://mysite/auction_details.php?name=' . $item_details["name"] . '&auction_id=' . $item_details["auction_id"] . '",

Those variables never got converted to their actual values that are supposed to be used. 这些变量从未转换为应该使用的实际值。 Try breaking that string into multiple parts via concatenation. 尝试通过串联将该字符串分成多个部分。

You'll want to end your echo quote so it's like this: 您将需要结束回声引用,如下所示:

echo '<script type="text/javascript"> 
....
            book: "http://mysite/auction_details.php?name=' . $item_details["name"] . '&auction_id=' . $item_details["auction_id"] . '",
....

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

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