accessing php session variable

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

accessing php session variable

Post by cjkeane »

Hi everyone.
I need to access a php session variable, so that when a button is clicked, the session variable is passed to the textarea. the text area is a tinymce editor.
I can pass the variable from one textarea to another, but i would prefer not to do it that way. this is my code so far. Any help would be appreciated. Thanks.

Code: Select all

<script language="javascript" type="text/javascript">
function addtext() {
	var signature = '<?php $_SESSION['SESS_USERS_SIG']; ?>';
	var newtext = "<br /><br />" + signature;
	tinymce.get('reply').execCommand('mceInsertContent',true, newtext); // adds the content at the carat postion
	
}
</script>
Add Signature to Message <input type="button" value="ADD" onclick="addtext();" />

Currently nothing happens when I click the button. if i replace var signature = '<?php $_SESSION['SESS_USERS_SIG']; ?>'; with var signature = 'TEST'; The word test appears in the tinymce editor, so I know the coding works with static text. I just can't seem to reference a php session variable correctly.
Last edited by cjkeane on Fri May 23, 2014 4:52 pm, edited 1 time in total.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: accessing php session variable

Post by requinix »

Just putting

Code: Select all

<?php $_SESSION['SESS_USERS_SIG']; ?>
won't actually do anything. If you want to output it into the Javascript then you have to output it into the Javascript.

But don't do it quite like that. Instead,

Code: Select all

var signature = <?php echo json_encode($_SESSION['SESS_USERS_SIG']); ?>;
This way you're sure to output valid Javascript - just putting the value in quotes could break your Javascript, which is to say nothing about how horribly insecure it would be.
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

Re: accessing php session variable

Post by cjkeane »

yes i was just reading about json_ecode, thanks :) this does work. one more thing, I need to replace \n with br in the $_SESSION['SESS_USERS_SIG'. is there an easy way to do this in javascript? i've figured it out. :) so my final code looks like:

Code: Select all

<script language="javascript" type="text/javascript">
function addtext() {
	var signature = <?php echo json_encode($_SESSION['SESS_USERS_SIG']); ?>;
	var newtext = "<br /><br />" + signature.replace(/\n/g, "<br />");;
	tinymce.get('reply').execCommand('mceInsertContent',true, newtext); // adds the content at the carat postion
	
}
</script>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: accessing php session variable

Post by requinix »

Personally I'd just do that before you output the text. Like

Code: Select all

var signature = <?php echo json_encode(nl2br($_SESSION['SESS_USERS_SIG'])); ?>;
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

Re: accessing php session variable

Post by cjkeane »

yes that is better. thanks!
katt
Forum Newbie
Posts: 1
Joined: Mon Nov 10, 2014 4:07 am

Re: accessing php session variable

Post by katt »

While you're trying to resolve the problem, it might be a good idea to put some alert()s in each branch of the two if() tests to see what's happening. Because of the way browsers and their graphics libraries work, a menu can be hidden in one part of the code and then displayed in another part of the code without it looking like it was ever hidden in the first place, because of the speed at which the operation is carried out by the JavaScript engine. If you put alerts()s before and after a specific action,
Post Reply