Save the choice made from a Drop/dwon menu?

Discussion in 'Javascript' started by JS, Jun 6, 2005.

  1. JS

    JS Guest

    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"??
     
    JS, Jun 6, 2005
    #1
    1. Advertising

  2. JS

    McKirahan Guest

    "JS" <> wrote in message news:d82jel$qkr$-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.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>
     
    McKirahan, Jun 7, 2005
    #2
    1. Advertising

  3. JS

    Ivo Guest

    "JS" <> wrote in message news:d82jel$qkr$-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
     
    Ivo, Jun 7, 2005
    #3
  4. JS

    McKirahan Guest

    "McKirahan" <> wrote in message
    news:...
    > "JS" <> wrote in message

    news:d82jel$qkr$-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!
     
    McKirahan, Jun 7, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vilmar
    Replies:
    0
    Views:
    506
    Vilmar
    May 27, 2004
  2. Mickey Segal
    Replies:
    0
    Views:
    945
    Mickey Segal
    Feb 2, 2004
  3. Steve Richter

    using asp:menu to create drop down menu

    Steve Richter, Jun 5, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    3,418
    Steve Richter
    Jun 5, 2007
  4. miles.jg
    Replies:
    16
    Views:
    925
    Alf P. Steinbach
    Nov 14, 2007
  5. Krustov

    Form box menu - force the drop down menu

    Krustov, Dec 5, 2006, in forum: Javascript
    Replies:
    2
    Views:
    188
    Krustov
    Dec 5, 2006
Loading...

Share This Page