0

I have a session variable load from a previous page, then I have a form with a Select box which allows a person to sign a document with either their first name, first and last name, or an alias. When that is chosen it activates the following function, which works fine as is ...

function jsignature()
{
var nsignature = document.getElementsByName("isignature")[0].value;
var signaturelist = [ "None Selected", "First name", "First & Lastname", "Known As" ];
var osignature = signaturelist[nsignature];   
document.getElementById("esign").innerHTML = osignature;
}

However, naturally I don't want the words "First name" etc, I want the actual first name.

I tried the following, which I have seen as an answer to this exact problem on 3 or 4 websites ...

function jsignature()
{
var nsignature = document.getElementsByName("isignature")[0].value;
var fname = '<%=Session["firstname"]%>';
var signaturelist = [ "None Selected", fname, "First & Lastname", "Known As" ];
var osignature = signaturelist[nsignature];   
document.getElementById("esign").innerHTML = osignature;
}

but the output was not the value stored in my session cookie "firstname" the output was actually "<%=Session["firstname"]%>"

I've also tried ...

var fname = <%=Session["firstname"]%>;
var fname = %=Session["firstname"]%;
var fname = Session["firstname"];

can anyone help please?

EDIT: In response to the "duplicate" issue, my question was about session variables not PHP variables, even though my solution was the PHP variable, however my solution is not the same solution in the alleged duplicate question.

Blackwolf
  • 93
  • 1
  • 12
  • The session is a server-side concept. You can extract values from it and *send them* to the client. Your JavaScript files are (apparently, and that's the normal case) not processed by your server-side template language (which you didn't name). – Pointy Mar 28 '16 at 04:42
  • i am using php, and at the top of my page i have $firstname=$_SESSION['firstname']; - can i transfer this to JS somehow ? – Blackwolf Mar 28 '16 at 04:45
  • Possible duplicate of [Access PHP variable in JavaScript](http://stackoverflow.com/questions/4287357/access-php-variable-in-javascript) – Alexei - check Codidact Mar 28 '16 at 04:53

2 Answers2

1

i am using php, and at the top of my page i have $firstname=$_SESSION['firstname']; - can i transfer this to JS somehow ?

?><script type="text/javascript">
  var firstname = <?php echo json_encode($firstname); %>;
</script><?php
Amadan
  • 191,408
  • 23
  • 240
  • 301
  • Thanks Amadan, that didn't work, but it inspired me and this did work ... var fname = ""; – Blackwolf Mar 28 '16 at 04:56
  • How specifically did it fail to work? Your solution will break if the variable has a quote character in it, or ends in a backslash... – Amadan Mar 28 '16 at 05:56
  • the variable is cleaned upon entry and only letters and hypens are allowed in names – Blackwolf Mar 28 '16 at 13:17
1

You cannot access session using Jquery. Because Session is at server side and Jquery works at client side.

What you can do is Store session in hidden field and then access it using Jquery.

Below code is for razor to store value hidden field :

<input type="hidden" id="hdnSession" data-value="@Request.RequestContext.HttpContext.Session["someKey"]" />

You can fetch value using Jquery as :

var sessionValue= $("#hdnSession").data('someKey');

If you want to get session value on load then its possible as

var someKey = '@Request.RequestContext.HttpContext.Session["someKey"]';

NOTE :: Once page is loaded you cannot get session value using jquery. But you can get its value on load.

maulik sakhare
  • 1,957
  • 1
  • 12
  • 19