PDA

View Full Version : Write session variable with javascript?


Gary Williams
09-02-2008, 11:27 AM
Hi All,

I have a form, divided into 3 div's, imaginatively named "div1", "div2" and "div3". When the page loads, "div1" displays and "div2" and "div3" remain hidden.

Div1 has a single dropdown, its' two choices are "div2" and "div3". Using javascript and its' "onChange" facility, I can use the dropdown to display Div2 (with Div3 hidden) or display Div3 (with Div2 hidden).

This works fine, but I also need to write an asp session variable. I need to write session("mode") = "Div2" (when Div2 displays) or session("mode") = "Div3" (when Div3 displays) whenever the dropdown choice changes.

Can this be achieved using javascript or test the dropdown value using asp?

I've checked similar posts/websites where similar solutions seem to have been developed but I can't adapt them to this problem.

Regards

Gary

hinch
09-02-2008, 08:37 PM
use a ajax style call back to load a seperate asp page something like createsession.asp and have that set the session.

you can't write directly to a server side session var with javascript so some how you have to make the page's load a different page in the background to write the session var

Gary Williams
09-02-2008, 09:38 PM
Hi Hinch,

"you can't write directly to a server side session var with javascript so some how you have to make the page's load a different page in the background to write the session var"

Ah, that I understand.

"use a ajax style call back to load a seperate asp page something like createsession.asp and have that set the session."

What's an ajax style call back and can this be done without leaving the page I've just loaded?

Regards

Gary

hinch
09-02-2008, 11:02 PM
yup ajax is exactly the thing that you do where it works without loading a page in the background.
basically you use ajax to load a "hidden" .asp page in the background its incrediably daunting at first indeed I've litterally 5 minutes ago just worked it out myself :) (using php though not asp)

its something like this so bear with me I'll try to make an example for you. Note I'm using the prototype javascript library google it you'll find it

displaypage.asp

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>Ajax test</title>
<script type="text/javascript" src="js/prototype.js"></script>
<script>
function setSession() {
var DivSelection = $F('DivSelector');
var url = './setsession.asp';
var pars = 'input=' + DivSelection ;
var myAjax = new Ajax.Updater({success: 'result'},url,
{
method:'post',
parameters: pars,
onFailure: reportError
});
}
<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>
<div id="result"></div>
<select id="DivSelector" size="10" onchange="showDiv();setSession()">
<option value="div1">Div1</option>
<option value="div2">Div2</option>
<option value="div3">Div3</option>
</select>


now you'll have to excuse me my ASP is very very rusty especially with sessions :)

setsession.asp

<%
Session.Timeout=5
sessionToSave = request.Form("input")
Session("SavedDiv")=sessionToSave
response.write "Saved selected Div to Session"
%>



Now if I've got that right :)

every time an option is selected from the dropdown it will do your show/hide javascript call (showdiv() ) and will also call the ajaxified function above setSession().

What this function does is put a post to the setsession.asp page and hopefully if my asp is correct the setsession.asp page saves the selected div from the dropdown into the session..

hinch
09-02-2008, 11:02 PM
hope that helps and probably more if it works if not I'm fairly sure there's some javascript/ajax guru's on here that can help you

Gary Williams
09-02-2008, 11:25 PM
Now that's impressive!

I'll paste it into my existing form and try it out.

Many thanks

Gary

hinch
09-02-2008, 11:30 PM
remember to grab a copy of the prototype.js library. and change <script type="text/javascript" src="js/prototype.js"></script> to match its location

also change the onchange="showDiv();setSession()">
to match the name of your existing show/hide function i used showDiv() as a placeholder yours probs callled something different

add this just above the </script> too

function reportError(request)
{
alert('Sorry. There was an error.');
}

Gary Williams
09-02-2008, 11:49 PM
Yes, did all that. When trying the app. I got an IE warning that "a script is causing IE to run slowly. Stop the script running? Yes/No".

What do I need running on the server to keep ajax happy?

Regards

Gary

hinch
09-02-2008, 11:56 PM
nothing its all client side :)

make sure your running the latest version of IE and make sure your using the latest version of the prototype library. http://prototypejs.org/

failing that try something like firefox with firebug installed see if you can find out whats causing the errors where.

unfortunately I can't test the code out above since I don't have IIS installed (on linux) however I did quickly test it against some php code and it works just fine. The problem could be with your previous hide/show div code or there could be a conflict in there somewhere. but the error you got was deffo to do with IE processing javascript.

Gary Williams
09-15-2008, 05:19 PM
Hi Hinch,

Fixed it! The problem was a conflict with the navigation buttons' rollover script. I got rid of that and everything works at normal speed now.

Using your solution, I can now write a session variable and open the relevant div. Problem is, to use the new session variable value, I need to reload the whole page to run the SQL scripts. Kind of defeats the whole object of the exercise! Using your technique, can you think how to re-run a chosen sql script to get the relevant recordset for the session variable selected, without reloading the whole page?

Regards

Gary