将PHP变量传递给JavaScript函数

时间:2013-03-21 22:34:53

标签: php javascript

我有以下代码:

FB.getLoginStatus(function (response) {
    if (response.status === "connected") {
        FB.api("me/bookvote:download", "post", {

        }, //Missing a , here?

然而,我仍然得到:

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

我的php变量到JavacScript有什么问题?

3 个答案:

答案 0 :(得分:3)

在字符串中的数组项周围包裹{},例如{$item_details["name"]}

答案 1 :(得分:1)

不再支持魔术引号。改变

book: "<?php echo "http://mysite.xom/auction_details.php?name=$item_details["name"]&auction_id=$item_details["auction_id"]";?>",

book: "<?php echo "http://mysite.xom/auction_details.php?name=" . $item_details["name"] . "&auction_id=" . $item_details["auction_id"];?>",

答案 2 :(得分:1)

您的PHP代码中没有正确连接:

FB.getLoginStatus(function (response) {
    if (response.status === "connected") {
        FB.api("me/bookvote:download", "post", {
            book: "<?php echo 'http://mysite.xom/auction_details.php?name='. $item_details['name'] . '&auction_id=' . $item_details['auction_id']; ?>",
            fb:explicitly_shared = "true" //? Is syntactically valid but creates a global
        },

为了便于阅读,我用单引号替换了PHP代码中的双引号。