0

My problem is fairly simple but somehow I was able to not get it to work, even by searching on google. I have a php variable that I would like to use in javascript. I tried to do something like

var fullLink = <?php echo $_SESSION['fullLink']; ?>;

but nope I got a "Uncaught syntaxerror : Unexpected Token" error so I guessed it was because of the php tags or somewhere near there. Then I tried in AJAX, but then I keep getting the full html. I tried creating another file just to test and see what was the problem but I was able to get the variable which says that it's my first file that has a problem, but dunno what. I'm pretty sure it's a frequent error but wasn't successful on getting an answer on google.

Edit 1 : Alright to be more straightforward, I'm trying to build dynamically a link on a blog I'm coding that will allow the user to share the post on facebook. Don't wanna use the "like" plugin from Facebook but the share (sharer.php) and then do a window.open() with the link. The problem is building the link with the title, mini-description and link of the blog post.

Thanks !

4 Answers 4

2

you missed " operator there

var fullLink = "<?php echo $_SESSION['fullLink']; ?>";
  //-----------^------------------------------------^ here

this will print the fulllink as string and var fullLink will get that value as string...

Sign up to request clarification or add additional context in comments.

5 Comments

I don'T see what you mean there. I tried to write the php code within the " " and without it, still same result.
are you sure your are getting the $_SESSION value.. if yes then this should work no doubt... i think , the problem is in session variable here...
As @bipen said - if the " " aren't working then you have a problem with the actual session variable
I added the quotes and it's saying Unexpected token illegal on that line
its not that code that is giving you error .. its the unwanted spaces or the new lines inside the quote..have a look to this stackoverflow.com/questions/8612344/… .. this might solve your problem
1

Providing this is within a PHP file you'll just need to add the " operator.

var fullLink = "<?php echo $_SESSION['fullLink']; ?>";

That will work providing that $_SESSION['fullLink'] exists.

UPDATE

There must be some other issue in your code, I have just created a test script using the fullLink from your source example, and the window.open() code you provided. It worked fine with just these parts:

<script>
    var fullLink = "http://www.facebook.com/sharer.php?s=100&p[url]=localhost:8080/BetaFolioBlogOOP??/Blog/post/5-2e-post&p[title]=2e post&p[summary]= Voici mon deuxi&egrave;me post question de voir si le tout marche bien ! ";
    window.open(fullLink, "Facebook_share", "menubar=1,resizable=1,width=600,height=500");      
</script>

15 Comments

Yes it is, I just didn't write the whole code since there's at least 150 lines in total.
That's fine then, you just need to add the quotes. :)
Now it's saying Unexpected token illegal. So my question may be dumb but what are the illegal token possible ? FYI I edited the post to give more explanation.
Just tried to copy the same exact string I'm supposed to receive in another file that I use only to test it and everything goes smoothly. So there must be something else that interfere, no ?
If you view source i.e. all the HTML/JavaScript in the browser. What do you see for the line in question?
|
0

Try

var fullLink = <?php echo json_encode($_SESSION['fullLink']); ?>;

Comments

0

PHP : calling javascript function

func($_SESSION['fullLink']);

or better:

if (isset($_SESSION['fullLink']))
{ func($_SESSION['fullLink']); }

Javascript:

function func(link){

var fulllink = link;
...}

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.