-2

I have a Javascript file main.js and I need to use echo $_SESSION["username"] within it, but it doesn't work obviously. Is there any way I can do this / some kind of workaround?

2
  • PHP will not be executed on a .js file. Commented Jun 11, 2015 at 11:00
  • 1
    You have the option to do a ajax call to retrieve the session value you need Commented Jun 11, 2015 at 11:00

2 Answers 2

2

You cannot execute PHP code in a .js file but there are ways to work around it. You could use an ajax call to get that variable (check out this link for ajax tips).

Depending on the information you wish to store, you could also get PHP to echo it into a specific element in your html page and then use JS to look up that element like this:

echo "<span id='usernameForJS' style='display: none;'>".$_SESSION['USERNAME']."</span>";

And then go and grab that elements inner html with JS like this:

var username = document.getElementById('usernameForJS').innerHTML;

I would advise AJAX just because then there is no need for almost redundant elements in your page. And obviously don't use the second method for anything sensitive.

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

4 Comments

I've been doing this but moved all of the Javascript into its own file because it was getting too long. Can I on log in, redirect them to a html page that does that and sets a cookie using JS, then straight after redirect them back to the main page where the main.js can then access that cookie value?
You could, yes. You would just need to use PHP header() to redirect you to the page you want to do it. I'm not entirely sure why you want to do this on a different page when you could do it at the top of the current page but maybe I'm missing something.
Wait can JS set in another file be accessed in main.js?
This answer might help. The scope of your JS is defined by the order that they are loaded. You obviously can't do something like go(); function go(){ alert();} because when you call go(); you haven't actually defined what go(); it is yet. If I had two files included in <script> tags, first.js and second.js and the first had the function declaration in and the second called the function, it would work as expected.
0

There is a option I saw on css-tricks.com

https://css-tricks.com/snippets/htaccess/use-php-inside-javascript/

I have never tried on my own but commentators said it works.

There is also a explanation of how you could do this in the comments: https://css-tricks.com/snippets/htaccess/use-php-inside-javascript/#comment-85757

But don't forget taht this is a rather "hacky" way to do it, you should consider to open that javascript file and instantiate your class/function(or whatever is in this file) from the .php-page.

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.