2013-03-21 142 views
0

我有以下代碼:傳遞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?

+1

看看代碼中的顏色,你確定你有這些引號嗎? – adeneo 2013-03-21 22:36:47

+0

啊對不起,我的編輯器很爛並且沒有突出顯示>< – user2185501 2013-03-21 22:39:41

+0

檢查瀏覽器中呈現的內容 – 2013-03-21 22:41:58

回答

3

裹{}在你的字符串中的陣列項目,如{$item_details["name"]}

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"];?>",

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代碼代替雙引號用單引號。