468,514 Members | 1,647 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,514 developers. It's quick & easy.

asp cookie to retain multiple select option state on page refresh

120 100+
I have a 'city' select dropdown which populates a 'hotel' select dropdown via js. then using an onchange event handler on the 'hotel' select to populate the content of an iframe. all working so far.

problem is when I refresh the page I lose the current state of the selected city, hotel.

from what I've read I need to use a cookie to retain the state. can someone please advise on how I can go about this?

Expand|Select|Wrap|Line Numbers
  1. function loadHotel() { 
  2.     var destURL = document.hotelslist.hotelnames.options[document.hotelslist.hotelnames.selectedIndex].value; 
  3.         window.frames["hotelframe"].location = destURL; }
  4.  
  5. var i;
  6. function hotel_list(i){
  7. var hotelslist = document.getElementById("hotelslist");
  8.     hotelslist.hotelnames.options.length=0;
  9.  
  10. switch(i)
  11. {    
  12. case 0: //Baddeck
  13. hotelslist.hotelnames.options[0] =new  Option('Inverary Resort, Baddeck','http://www.canadianaffair.com/en/hotels/atlantic-canada/baddeck/inverary-resort/');
  14. break;
  15. case 1: //Banff
  16. hotelslist.hotelnames.options[0] =new  Option('Banff Caribou Lodge & Spa','http://www.canadianaffair.com/en/hotels/alberta/banff/banff-caribou-lodge-and-spa/');
  17. hotelslist.hotelnames.options[1] =new  Option('Banff Ptarmigan Inn','http://www.canadianaffair.com/en/hotels/alberta/banff/banff-ptarmigan-inn/');
  18. break;
  19. case 2: //Calgary
  20. hotelslist.hotelnames.options[0] =new  Option('Sandman - Calgary Downtown','http://www.canadianaffair.com/en/hotels/alberta/calgary/sandman-calgary-downtown/');
  21. hotelslist.hotelnames.options[1] =new  Option('The Westin Calgary','http://www.canadianaffair.com/en/hotels/alberta/calgary/the-westin-calgary/');
  22. break;
  23. }
  24. }
  25.  
  26. <td class="hotelslisttd">
  27. <form name="hotelslist" id="hotelslist">
  28. <select id="cities" name="cities" onChange="hotel_list(hotelslist.cities.selectedIndex);loadHotel();">
  29. <option value="Baddeck">Baddeck</option>
  30. <option value="Banff">Banff</option>
  31. <option value="Calgary">Calgary</option>
  32. </select>
  33. </td>
  34. <td class="hotelnamestd">
  35. <select id="hotelnames" name="hotelnames" onchange="loadHotel();";>
  36. <option value="Inverary Resort, Baddeck">Inverary Resort, Baddeck</option>
  37. </select>
  38. </form></td>
  39.  
  40. <iframe name="hotelframe" id="hotelframe" src="http://www.canadianaffair.com/en/hotels/atlantic-canada/baddeck/inverary-resort/" scrolling="no" width="560" height="1000" marginwidth="0" marginheight="0" hspace="0" vspace="0" frameborder="0"> <p>Your browser does not support iframes.</p> </iframe>
  41.  
  42.  
thanks in advance
Omar.
Apr 19 '10 #1
15 2978
jhardman
3,405 Expert 2GB
yeah, setting a cookie would work, I would do it on the iFrame page
Expand|Select|Wrap|Line Numbers
  1. response.cookies("city") = request.form("city")
or you could just set it as a session variable
Expand|Select|Wrap|Line Numbers
  1. session("city") = request.form("city")
Jared
Apr 20 '10 #2
omar999
120 100+
thank you for starting me off Jared.
I've added this to top of the page containing form, selects & iframe
Expand|Select|Wrap|Line Numbers
  1. <%
  2. response.cookies("cities") = request.form("cities")
  3. %>
then within the iframe src page I've added this to the top of the page
Expand|Select|Wrap|Line Numbers
  1. <%
  2. city=Request.Cookies("cities")
  3. response.write("cities=" & city)
  4. %>
  5.  
but it's not outputting the selected city? has this something to do with my iframe src?...
Apr 20 '10 #3
jhardman
3,405 Expert 2GB
@omar999
it is the iframe page that gets reloaded when the city is selected, right? then you need to set the city cookie there.

Jared
Apr 20 '10 #4
omar999
120 100+
hi jared - the iframe reloads when city & hotel are selected or "onchange".

so if i understand correct do you mean i should set and retrieve the cookie within the iframe?
Apr 20 '10 #5
jhardman
3,405 Expert 2GB
@jhardman
yes, at least you need to set it there within the iframe (that's where the city input is sent when the input is changed, so that's the best place to set the cookie).

If I understand what you are asking, you would need to retrieve it on the main page (if it exists) - if the cookie exists when the main page is being loaded, then you have previously changed the hotel and city selects and now are just refreshing that page. Does this make sense?

Jared
Apr 20 '10 #6
omar999
120 100+
ok cool I will set & retrieve cookie within iframe and will post back progress.

yea basically on a page refresh I dont want the city, hotel & iframe src to change from what the user may have already selected - instead I want to retain the selected state if the page is refreshed.

Omar.
Apr 20 '10 #7
omar999
120 100+
set & retreived cookie within iframe src but no joy. to clarify - this is my setup;

default.asp [contains form, selects & iframe - (iframe src pointing to hotel1.asp)]
then the function hotel_list populates the iframe src of every other hotel - e.g hotel2.asp, hotel3.asp..etc

not sure where to go from here... please advise
Apr 20 '10 #8
jhardman
3,405 Expert 2GB
@omar999
YOU DO NOT NEED TO RETRIEVE THE COOKIE IN THE IFRAME. YOU NEED TO SET THE COOKIE IN THE IFRAME AND RETRIEVE IT IN THE MAIN PAGE. IT WON"T WORK IN ANY OTHER COMBINATION.

Jared
Apr 21 '10 #9
omar999
120 100+
morning Jared

thanks for clarifying - my brain is a little fried so I'm finding it hard to process anything. it doesnt help when I've been working 15 hour shifts 2 days in a row!

I'll give this a go and will post up progress. thank u
Apr 21 '10 #10
omar999
120 100+
ok I've set the cookie in the iframe as
Expand|Select|Wrap|Line Numbers
  1. <%response.cookies("cities") = request.form("cities")%>
and to retrieve in main page as
Expand|Select|Wrap|Line Numbers
  1. <%city=Request.Cookies("cities")
  2. response.write("cities=" & city)%>
but still no joy. I've tried refreshing the page and selecting different hotels but the city value isnt being displayed on the page?
Apr 21 '10 #11
jhardman
3,405 Expert 2GB
@omar999
try to list everything in cookies just to clarify whether cookies are being passed:
Expand|Select|Wrap|Line Numbers
  1. response.write "<p>Cookies:" & vbNewLine
  2. for each x in request.cookies
  3.    response.write x & ": " & request.cookies(x) & "<br>" & vbNewLine
  4. next
Jared
Apr 21 '10 #12
omar999
120 100+
hi jared

I added this code to the main page and the page is displaying this [i've edited out the website address]

Expand|Select|Wrap|Line Numbers
  1. Cookies: __utma: 228366991.358854659.1270736550.1271799773.1271836851.52
  2. __utmz: 228366991%2E1270736550%2E1%2E1%2Eutmcsr=%28direct%29%7Cutmccn%3D%28direct%29%7Cutmcmd%3D%28none%29
  3. cities:
  4. name:
  5. hasVisited: 0
  6. __utmc: 1
  7. X-Mapping-gdmmbdpb: B7FC7B07D3E4A6F2410CF7CBA64192E5
  8. __utmb: 228366991.323.10.1271836851
  9. profile_referrer: websitename
  10. websitenameGAId: UA-6277292-1
  11. firstname: Alex
  12. hotelnames: 
  13.  
not sure what this means?
Apr 22 '10 #13
jhardman
3,405 Expert 2GB
a lot of it you can just ignore, but on the 5 you set (cities, name, hasVisited, firstname, hotelnames) only two had values, the rest of the cookies were set, but had no values. I bet that when you refresh the page you are also clearing the values of those cookies. When you go to set the cookies check to see if you are setting an empty value:
Expand|Select|Wrap|Line Numbers
  1. if request.form("cities") <> "" then
  2.    response.cookies("cities") = request.form("cities")
  3. end if 
Jared
Apr 22 '10 #14
omar999
120 100+
hi jared

I added
Expand|Select|Wrap|Line Numbers
  1. if request.form("cities") <> "" then
  2.    response.cookies("cities") = request.form("cities")
  3. end if 
to the page and refreshed it and the page still outputs this same message
Expand|Select|Wrap|Line Numbers
  1. Cookies: __utma: 228366991.358854659.1270736550.1271799773.1271836851.52
  2. __utmz: 228366991%2E1270736550%2E1%2E1%2Eutmcsr=%28direct%29%7Cutmccn%3D%28direct%29%7Cutmcmd%3D%28none%29
  3. cities:
  4. name:
  5. hasVisited: 0
  6. __utmc: 1
  7. X-Mapping-gdmmbdpb: B7FC7B07D3E4A6F2410CF7CBA64192E5
  8. __utmb: 228366991.323.10.1271836851
  9. profile_referrer: websitename
  10. websitenameGAId: UA-6277292-1
  11. firstname: Alex
  12. hotelnames: 
  13.  
i wish there was a senior developer at my company. this sucks.
Apr 23 '10 #15
jhardman
3,405 Expert 2GB
is this live? Can I browse there to check it?

Jared
Apr 23 '10 #16

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

7 posts views Thread by ehendrikd | last post: by
4 posts views Thread by darrel | last post: by
3 posts views Thread by ANTISPAM_garycnew_ANTISPAM | last post: by
2 posts views Thread by areef.islam | last post: by
2 posts views Thread by Griff | last post: by
5 posts views Thread by Jon Davis | last post: by
3 posts views Thread by Dan | last post: by
4 posts views Thread by rn5a | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.