Stealthunter
Stealthunter

Reputation: 21

jQuery : getting PHP variables by AJAX

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 !

Upvotes: 0

Views: 252

Answers (4)

diggersworld
diggersworld

Reputation: 13080

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>

Upvotes: 1

Amir
Amir

Reputation: 4111

PHP : calling javascript function

func($_SESSION['fullLink']);

or better:

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

Javascript:

function func(link){

var fulllink = link;
...}

Upvotes: 0

Musa
Musa

Reputation: 97672

Try

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

Upvotes: 0

bipen
bipen

Reputation: 36531

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...

Upvotes: 2

Related Questions