Jump to content

How do I force javascript to view data as a string?


Arbu
 Share

Recommended Posts

I have some data in the form "I-AHUSH" (it's a Paypal subscription reference). I'm saving it as a $_SESSION variable on the server. When I recover it and do the following, javascript insists on treating it as something to be evaluated. This causes an error. How do I stop it from doing so? You can see that I've tried forcing it to be a string. But that doesn't help.

var s = String(<?php echo $_SESSION['subscriptionid'] ?>);

Thanks

Link to comment
Share on other sites

PHP and Javascript operate in two completely different environments. PHP generates Javascript code and Javascript is unaware that PHP was running at all.

You could even write something this and it could potentially generate usable Javascript:

var <?php echo $_SESSION['varname'] ?> = "Hello World!";

 

In order to have Javascript treat something as a string, it has to be wrapped in quotation marks. On PHP's end, you'll want to escape any quotation marks that are in the string itself. A working example should look something like this:

var s = "<?php echo addslashes($_SESSION['subscriptionid']); ?>";

 

Link to comment
Share on other sites

  • 1 month later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...