简体   繁体   中英

How do I get Javascript to get a Session variable value

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.

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

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.

Thanks Amadan, you inspired me ... this works perfectly ...

function jsignature()
{
var nsignature = document.getElementsByName("isignature")[0].value;
var firstname = "<?php echo $firstname; ?>";
var bothnames = "<?php echo $firstname." ".$lastname; ?>";
var knownas = "<?php echo $knownas; ?>";
var signaturelist = [ "None Selected", firstname, bothnames, knownas ];
var osignature = signaturelist[nsignature];   
document.getElementById("esign").innerHTML = osignature;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM