Page 1 of 1

accessing php session variable

Posted: Fri May 23, 2014 4:23 pm
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.

Re: accessing php session variable

Posted: Fri May 23, 2014 4:50 pm
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.

Re: accessing php session variable

Posted: Fri May 23, 2014 4:55 pm
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>

Re: accessing php session variable

Posted: Fri May 23, 2014 5:38 pm
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'])); ?>;

Re: accessing php session variable

Posted: Fri May 23, 2014 6:08 pm
by cjkeane
yes that is better. thanks!

Re: accessing php session variable

Posted: Mon Nov 10, 2014 4:11 am
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,