469,898 Members | 2,033 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Save the choice made from a Drop/dwon menu?

JS
I have two drop/down menus that are dependant on each other. When I have
made a choice in both of these menus and press a button I get to a page
depending on the choice made. On this page there is two buttons "Accept" and
"Back".

When I press "Back" I would like to get back to the page with the two menus
and they should be set to the choice I made. Now the just reset when I press
the "Back" button.

Is it possible to save the options that I choose from the drop/down menus
when I get back to them by pressing "Back"??
Jul 23 '05 #1
3 1620
"JS" <dsa.@asdf.com> wrote in message news:d8**********@news.net.uni-c.dk...
I have two drop/down menus that are dependant on each other. When I have
made a choice in both of these menus and press a button I get to a page
depending on the choice made. On this page there is two buttons "Accept" and "Back".

When I press "Back" I would like to get back to the page with the two menus and they should be set to the choice I made. Now the just reset when I press the "Back" button.

Is it possible to save the options that I choose from the drop/down menus
when I get back to them by pressing "Back"??


Here's one solution. Watch for word-wrap.

<< page1.htm >>

<html>
<head>
<title><title>page1.htm</title>
<script type="text/javascript">
function submits(form) {
var form = document.form1;
var ind1 = form.sel1.selectedIndex;
var ind2 = form.sel2.selectedIndex;
if (ind1 == 0 || ind2 == 0) return false;
var sel1 = form.sel1.options[ind1].value;
var sel2 = form.sel2.options[ind2].value;
document.action += "?" + sel1 + "&" + sel2;
return true;
}
function selects() {
var qstr = location.search;
if (qstr.length < 4) {
alert("Invalid QueryString!");
return;
}
var form = document.form1;
qstr = qstr.substr(1);
var pair = qstr.split("&");
for (var i=0; i<pair.length; i++) {
var item = pair[i].split("=");
if (i == 0) {
for (var j=1; j<form.sel1.length; j++)
if (form.sel1.options[j].value == item[1]) {
form.sel1[j].selected = true;
}
}
if (i == 1) {
for (var j=1; j<form.sel2.length; j++)
if (form.sel2.options[j].value == item[1]) {
form.sel2[j].selected = true;
}
}
}
}
</script>
</head>
<body onload="selects()">
<form action="page2.htm" method="get" name="form1" onsubmit="return
submits()">
<br>
<select name="sel1">
<option value="" selected>
<option value="1">One
<option value="2">Two
<option value="3">Three
</select>
<br>
<select name="sel2">
<option value="" selected>
<option value="a">A
<option value="b">B
<option value="c">C
</select>
<br>
<input type="Submit" value="Submit">
</form>
</body>
</html>
<< page2.htm >>

<html>
<head>
<title>page2.htm</title>
<script type="text/javascript">
function backs() {
location.href = "page1.htm" + location.search;
}
function texts() {
var qstr = location.search;
if (qstr.length < 4) return;
var form = document.form1;
qstr = qstr.substr(1);
var pair = qstr.split("&");
for (var i=0; i<pair.length; i++) {
var item = pair[i].split("=");
if (i == 0) form.sel1.value = item[1];
if (i == 1) form.sel2.value = item[1];
}
}
</script>
</head>
<body onload="texts()">
<form action="page3.htm" method="get" name="form1">
<br>
<input type="text" name="sel1" readonly>
<br>
<input type="text" name="sel2" readonly>
<br>
<input type="submit" value="Accept">
<input type="button" value="Back" onclick="backs()">
</form>
</body>
</html>

<< page3.htm >>

<html>
<head>
<title>page3.htm</title>
</head>
<body>
<b>Accepted</b>
</body>
</html>
Jul 23 '05 #2
Ivo
"JS" <dsa.@asdf.com> wrote in message news:d8**********@news.net.uni-c.dk...
I have two drop/down menus that are dependant on each other. When I have
made a choice in both of these menus and press a button I get to a page
depending on the choice made. On this page there is two buttons "Accept"
and "Back".
Is it possible to save the options that I choose from the drop/down menus
when I get back to them by pressing "Back"??


What is the code behind that Back button (and why do you need one when all
known browsers provide the very thing with plenty of prominence)? If it is
to be the same on each of those selectable pages, the code probably looks
quite generic, like

onclick="history.go(-1)"

and you need to resort to setting cookies on the originating page:

<select onchange=" dostuff(); alsosetacookie(); ">

For example http://4umi.com/web/css/changesheet.htm
contains some hilited cookie writing and reading code.

But if you can code specific url's on specific pages (a serverside script
perhaps?), something like

onclick="location.href='lastpage.html?a=5&amp;b=7' ;"

then you can read which option to select in which list from the url.
A quick n dirry approach, with a and b being references to the
respective <select> boxes:

var s = window.location.search;
if( (i = s.match( /a=(\d+)/ ) ) ) {
a.options[ i[1] ].selected=true;
}
if( (i = s.match( /b=(\d+)/ ) ) ) {
b.options[ i[1] ].selected=true;
}

should get you on the way...

hth
Ivo
Jul 23 '05 #3
"McKirahan" <Ne**@McKirahan.com> wrote in message
news:CP********************@comcast.com...
"JS" <dsa.@asdf.com> wrote in message

news:d8**********@news.net.uni-c.dk...
I have two drop/down menus that are dependant on each other. When I have
made a choice in both of these menus and press a button I get to a page
depending on the choice made. On this page there is two buttons "Accept"

and
"Back".

When I press "Back" I would like to get back to the page with the two

menus
and they should be set to the choice I made. Now the just reset when I

press
the "Back" button.

Is it possible to save the options that I choose from the drop/down menus when I get back to them by pressing "Back"??


Can you use ASP -- it would be a lot easier to do it server-side!
Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by hemanth.singamsetty | last post: by
40 posts views Thread by Mark G. Meyers | last post: by
3 posts views Thread by rsteph | last post: by
4 posts views Thread by TycoonUK | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.