0

I have an php script that is echoing JavaScript, in the JavaScript there are PHP variables

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?

3
  • Is this in a .js file, if so does it get parsed by PHP ? Commented Mar 21, 2013 at 22:59
  • 1
    You are using a tag <?php inside php echo function. It should be like book: "http://......'.$item_details.'...." Commented Mar 21, 2013 at 23:01
  • possible duplicate of Passing PHP Variable into JavaScript Function Commented Mar 22, 2013 at 0:35

4 Answers 4

3

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.
  • You try to use additional <?php ?> tags inside of your echo. That would obviously not work.

Try this. I have removed the echo. Note that the larger PHP tag ends there.

?>
<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?
Sign up to request clarification or add additional context in comments.

1 Comment

I have no idea if this works, but I'll upvote it just on the "don't use echo to print HTML/JavaScript" line. Seems like a great idea.
0

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"] . '",
....

Comments

0

Change this line:

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

Comments

0

Those variables never got converted to their actual values that are supposed to be used. Try breaking that string into multiple parts via concatenation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.