Save the choice made from a Drop/dwon menu?

J

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"??
 
M

McKirahan

JS said:
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.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.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>
 
I

Ivo

JS said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top