Arbu Posted June 17, 2021 Share Posted June 17, 2021 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 More sharing options...
Ingolme Posted June 17, 2021 Share Posted June 17, 2021 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 More sharing options...
Arbu Posted June 18, 2021 Author Share Posted June 18, 2021 I see. Thanks very much. Link to comment Share on other sites More sharing options...
Arbu Posted July 20, 2021 Author Share Posted July 20, 2021 Slightly disconcertingly, though, I find that Notepad++, which I use, doesn't show the code formatting colours when I put the inverted commas around the php. This makes it look as if the code won't work, although clearly it does. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now