Saving the selection of a select element when the User clicks the back button.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • PM ArunKumar
    New Member
    • Sep 2007
    • 2

    Saving the selection of a select element when the User clicks the back button.

    i have two dropdown list in my form where the values in the list of second drop down changes based on the value i select in the first dropdown.(very similar to country and states list), here after submitting the form, if i go back the value which i selected in my second dropdown list doesn't appear. for eg: if select USA and virginia ,when i go back USA appears but virginia doesn't appear in my page.
    can any help me to retain the value.
    i will appreciate all the help provided.
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    #2
    Originally posted by PM ArunKumar
    i have two dropdown list in my form where the values in the list of second drop down changes based on the value i select in the first dropdown.(very similar to country and states list), here after submitting the form, if i go back the value which i selected in my second dropdown list doesn't appear. for eg: if select USA and virginia ,when i go back USA appears but virginia doesn't appear in my page.
    can any help me to retain the value.
    i will appreciate all the help provided.

    Welcome to TSDN!
    Post some code what you done so far using Code Tags.
    Then state the actual problem you facing.
    Then we will able to solve your problem.

    Kind regards,
    Dmjpro.

    Comment

    • PM ArunKumar
      New Member
      • Sep 2007
      • 2

      #3
      dynamic dropdown list

      in below code if we select some values and submit it works fine. but if i come back using back button. the values which i selected doesn't appear, can anyone tell me the way to retain the value.

      ALL SORT OF HELP IS APPRECIATED

      [HTML]<script language="JavaS cript" type="text/javascript">
      <!--

      /*
      *** Multiple dynamic combo boxes
      *** by Mirko Elviro, 9 Mar 2005
      *** Script featured and available on JavaScript Kit (http://www.javascriptk it.com)
      ***
      ***Please do not remove this comment
      */

      // This script supports an unlimited number of linked combo boxed
      // Their id must be "combo_0", "combo_1", "combo_2" etc.
      // Here you have to put the data that will fill the combo boxes
      // ie. data_2_1 will be the first option in the second combo box
      // when the first combo box has the second option selected


      // first combo box

      data_1 = new Option("1", "$");
      data_2 = new Option("2", "$$");

      // second combo box

      data_1_1 = new Option("11", "-");
      data_1_2 = new Option("12", "-");
      data_2_1 = new Option("21", "--");
      data_2_2 = new Option("22", "--");
      data_2_3 = new Option("23", "--");
      data_2_4 = new Option("24", "--");
      data_2_5 = new Option("25", "--");

      // third combo box

      data_1_1_1 = new Option("111", "*");
      data_1_1_2 = new Option("112", "*");
      data_1_1_3 = new Option("113", "*");
      data_1_2_1 = new Option("121", "*");
      data_1_2_2 = new Option("122", "*");
      data_1_2_3 = new Option("123", "*");
      data_1_2_4 = new Option("124", "*");
      data_2_1_1 = new Option("211", "**");
      data_2_1_2 = new Option("212", "**");
      data_2_2_1 = new Option("221", "**");
      data_2_2_2 = new Option("222", "**");
      data_2_3_1 = new Option("231", "***");
      data_2_3_2 = new Option("232", "***");

      // fourth combo box

      data_2_2_1_1 = new Option("2211"," %")
      data_2_2_1_2 = new Option("2212"," %%")

      // other parameters

      displaywhenempt y=""
      valuewhenempty=-1

      displaywhennote mpty="-select-"
      valuewhennotemp ty=0


      function change(currentb ox) {
      numb = currentbox.id.s plit("_");
      currentbox = numb[1];

      i=parseInt(curr entbox)+1

      // I empty all combo boxes following the current one

      while ((eval("typeof( document.getEle mentById(\"comb o_"+i+"\"))!='u ndefined'")) &&
      (document.getEl ementById("comb o_"+i)!=null) ) {
      son = document.getEle mentById("combo _"+i);
      // I empty all options except the first one (it isn't allowed)
      for (m=son.options. length-1;m>0;m--) son.options[m]=null;
      // I reset the first option
      son.options[0]=new Option(displayw henempty,valuew henempty)
      i=i+1
      }


      // now I create the string with the "base" name ("stringa"), ie. "data_1_0"
      // to which I'll add _0,_1,_2,_3 etc to obtain the name of the combo box to fill

      stringa='data'
      i=0
      while ((eval("typeof( document.getEle mentById(\"comb o_"+i+"\"))!='u ndefined'")) &&
      (document.getEl ementById("comb o_"+i)!=null) ) {
      eval("stringa=s tringa+'_'+docu ment.getElement ById(\"combo_"+ i+"\").selected Index")
      if (i==currentbox) break;
      i=i+1
      }


      // filling the "son" combo (if exists)

      following=parse Int(currentbox) +1

      if ((eval("typeof( document.getEle mentById(\"comb o_"+following+" \"))!='undefine d'")) &&
      (document.getEl ementById("comb o_"+following)! =null)) {
      son = document.getEle mentById("combo _"+following );
      stringa=stringa +"_"
      i=0
      while ((eval("typeof( "+stringa+i+")! ='undefined'")) || (i==0)) {

      // if there are no options, I empty the first option of the "son" combo
      // otherwise I put "-select-" in it

      if ((i==0) && eval("typeof("+ stringa+"0)=='u ndefined'"))
      if (eval("typeof(" +stringa+"1)==' undefined'"))
      eval("son.optio ns[0]=new Option(displayw henempty,valuew henempty)")
      else
      eval("son.optio ns[0]=new Option(displayw hennotempty,val uewhennotempty) ")
      else
      eval("son.optio ns["+i+"]=new Option("+string a+i+".text,"+st ringa+i+".value )")
      i=i+1
      }
      //son.focus()
      i=1
      combostatus=''
      cstatus=stringa .split("_")
      while (cstatus[i]!=null) {
      combostatus=com bostatus+cstatu s[i]
      i=i+1
      }
      return combostatus;
      }
      }

      //-->
      </script>

      <form>
      <select name="combo0" id="combo_0" onChange="chang e(this);" style="width:20 0px;">
      <option value="value1">-select-</option>
      <option value="value2"> 1</option>
      <option value="value3"> 2</option>

      </select>
      <BR><BR>
      <select name="combo1" id="combo_1" onChange="chang e(this)" style="width:20 0px;">
      <option value="value1"> </option>
      </select>
      <BR><BR>
      <select name="combo2" id="combo_2" onChange="chang e(this);" style="width:20 0px;">
      <option value="value1"> </option>
      </select>
      <BR><BR>
      <select name="combo3" id="combo_3" onChange="chang e(this);" style="width:20 0px;">
      <option value="value1"> </option>

      </select>

      </form>

      <p align="center"> <font face="arial" size="-2">This free script provided by</font><br>
      <font face="arial, helvetica" size="-2"><a href="http://javascriptkit.c om">JavaScrip t
      Kit</a></font></p>[/HTML]
      Last edited by gits; Sep 20 '07, 10:54 AM. Reason: added code tags

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Merged duplicate threads.

        Changed thread title to better describe the problem (did you know that threads whose titles do not follow the Posting Guidelines actually get FEWER responses?).

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          Since you're using Javascript to dynamically create the options, they won't appear when you go back. You could use a cookie to save the state or call change() on the currently selected option on page load.

          Comment

          Working...