Session on Html

Whing Dela Cruz
Whing Dela Cruz used Ask the Experts™
on
Hi experts, I'm trying to change the value of Session inside the script but it doesn't work. Any help please!

<script>
function ChangeSession()
{
    Session("AllowT") = "T"
}
</script>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Ryan ChongSoftware Team Lead

Commented:
session should runs at server side not like javascript which runs on client side.

do you mean something like this?

<script>
function ChangeSession()
{
<%
    Session("AllowT") = "T"
%>
  var t = "<%=Session("AllowT")%>";
   alert("T = " + t);
}
</script>

Open in new window

Thank you Ryan, Before I close this question can I do something like variable declared in script and put the value in Session?

<script>
function ChangeSession()
{
  var T = "Tail"
<%
    Session("AllowT") = T;
%>
  var t = "<%=Session("AllowT")%>";
   alert("T = " + t); //here the alert should be "Tail"
}
</script>

Open in new window

Ryan ChongSoftware Team Lead

Commented:
can I do something like variable declared in script and put the value in Session?
you probably can't until you do a posting to server side.

to change something on Session, you need to do it on server side end.

hope this clarifies.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
You have to make a call back to the server to set the Session you can do this with an AJAX call that sends the value to a server side process that then sets the session something like this (using jQuery)

$.post('setSession.asp', {t: T})

Open in new window


Your script above indicates a slight misunderstanding on how javascript works.

When you run your function it all runs within the context of the browser - no server code is available.

Your code indicates you are trying to set a session variable on the server and then immediately after that output the value in JavaScript to show it has changed.

To do what you want to do you need to get a handle on some of the aspects of client server interaction in a web context. When you need to talk to the server you do so (for the most part) asynchronously - which means you send a request to the server and you carry on with other things. When the server returns you have code that is waiting to pick up the response and deal with it - but in the context of your application this could be a significant amount of time after your initial request was sent. To get around this we write event handlers (callbacks) that respond to the server return. This requires a bit of a shift in thinking because we need to suspend the code that needs the return and place that instead inside the callback.

To extend your example a little further. In this example we send a value to the server and the server simply sends that value back.
<script src="http://code.jquery.com/jquery.js"></script>
<script>
var T = 'Tail';
$(function() {
  $.post('setSession.asp',{t: T}, function(resp) {
     // This function is called when the server has sent its response
     // The resp parameter is whatever we decide to send back to
     // the calling application. In this case we are just mirroring
     // what was sent
     T = resp;
  });
});</script>

Open in new window

The asp for this would look something like
<%
Session("AllowT") = Request("t")
Response.Write Session("AllowT")
%>

Open in new window


Now this does not really tell us anything - because how do we know the value was actually set. Let's change it a bit so that we can tell what value was set on the server. This is tricky because we first have to set a value before we can test it but because we are running asynchronously so we can't just put the statements one after the other. We have two options
1. We put the test inside the callback from the set
2. We create a separate event handler that we control (like a button click) that tests the value for us

I am going to use 2
<button onclick="test()">Test</button>
<script src="http://code.jquery.com/jquery.js"></script>
<script>
var T = 'Tail';
$(function() {
  $.post('setSession.asp',{t: T}, function(resp) {
     // This function is called when the server has sent its response
     // The resp parameter is whatever we decide to send back to
     // the calling application. In this case we are just mirroring
     // what was sent
     T = resp;
  });
});
function test()
{
  $.get('testGet.asp',function(resp) {
    alert('Session: ' + resp);
  });
}
</script>

Open in new window


This will demonstrate that the value set in the $.post is in fact set on the server.

Author

Commented:
Thanks a lot Julian, your explanation gives more clearer to me. I will try it now!

Author

Commented:
Thank you both of guys, your  comments and suggestion helps me a lot to understand. More power to both of you and May God give more knowledge to help those who are in need!
Ryan ChongSoftware Team Lead

Commented:
@Whing Dela Cruz

are you accepting your own comment as the assisted answer?
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
You are welcome

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial