Session on Html

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

Whing Dela CruzAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
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

0
Whing Dela CruzAuthor Commented:
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

0
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
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.
0
Determine the Perfect Price for Your IT Services

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

Julian HansenCommented:
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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Whing Dela CruzAuthor Commented:
Thanks a lot Julian, your explanation gives more clearer to me. I will try it now!
0
Whing Dela CruzAuthor 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!
0
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
@Whing Dela Cruz

are you accepting your own comment as the assisted answer?
0
Julian HansenCommented:
You are welcome
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
HTML

From novice to tech pro — start learning today.