Mark H Posted March 25, 2011 Share Posted March 25, 2011 Hi all,I started this in another thread, but thought I'd start a new thread before the "hijack" of someone else's thread got too long.What I need to know is whether the JS on my page can access variables defined in PHP?e.g. If I set up $signedIn = 'true' using PHP, can I then "grab" that variable and use it in the JS on the page?I've had a quick look at AJAX, but that seems to cover data transfer more complex than a simple variable from PHP to JS.Thanks,Mark. Link to comment Share on other sites More sharing options...
chokk Posted March 25, 2011 Share Posted March 25, 2011 You could pass variables from PHP in the URL and read them from JavaScript using regular expressions. Link to comment Share on other sites More sharing options...
Ingolme Posted March 25, 2011 Share Posted March 25, 2011 There's no way to just access PHP variables. By the time Javascript is running, all the PHP data has been destroyed except for what has been sent to the client.You can use PHP to print out the variables that you want into some Javascript code. But by the time Javascript can use them, PHP will not be running anymore. Link to comment Share on other sites More sharing options...
Mark H Posted March 25, 2011 Author Share Posted March 25, 2011 There's no way to just access PHP variables. By the time Javascript is running, all the PHP data has been destroyed except for what has been sent to the client.You can use PHP to print out the variables that you want into some Javascript code. But by the time Javascript can use them, PHP will not be running anymore.Would I be able to use a _SESSION variable, and then use that variable in javascript using PHP within the javascript? Or would the PHP variable, even if a _SESSION, not be accessible to the browser? Link to comment Share on other sites More sharing options...
Ingolme Posted March 25, 2011 Share Posted March 25, 2011 $_SESSION is not available to the browser, but if you set a session variable you can access it with AJAX by having a separate PHP file that prints a specified session variable, or sets it based on GET or POST information sent by Javascript.There's no way around it without AJAX. Link to comment Share on other sites More sharing options...
birbal Posted March 25, 2011 Share Posted March 25, 2011 session is only accesible in php context. i am not sure why you need to acces the php variable in js. what you are trying to do actualy? Link to comment Share on other sites More sharing options...
Mark H Posted March 25, 2011 Author Share Posted March 25, 2011 Thanks for your help...I'll get back to looking at AJAX!birbal:I am wanting to use PHP for all processing of a mini forum. Yet, I was planning on using JS to create a number of <DIV>s (or, rather, change <DIV> CSS from display: hidden to display: block and vice versa) which would be different depending upon whether a user is logged in or not. I need the JS to be able to execute only if logged-in, or, in another instance, logged out.Thanks greatly! Link to comment Share on other sites More sharing options...
Ingolme Posted March 25, 2011 Share Posted March 25, 2011 Rather than using Javascript to show or hide divs, why not use PHP to decide whether they need to be printed or not?If the user has Javascript disabled they may be able to access parts of your site that you don't want them to see. Link to comment Share on other sites More sharing options...
birbal Posted March 25, 2011 Share Posted March 25, 2011 if i undersatnd the situation..why not surveing two content one if registered and another for guest. you can check in php file.. a user is loged in or not. if yes show the member part else guest part.if you want to strict the content for guest and member the display:block/hidden will not be suitable. cause disableing css or editing it can make it available to client browser. Link to comment Share on other sites More sharing options...
Mark H Posted March 25, 2011 Author Share Posted March 25, 2011 Rather than using Javascript to show or hide divs, why not use PHP to decide whether they need to be printed or not?If the user has Javascript disabled they may be able to access parts of your site that you don't want them to see.Thanks, I think that may well be the solution I need. I'm presuming from my limited knowledge and what you say that I can add such <DIV>s using PHP.One thing, though, is that I am creating a drop-down box, using Javascript to execute on mousedown. If I add such js to act only on a specific <DIV>, I would think that would be my solution. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.