store menu array data between forms

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • swebster
    New Member
    • Jun 2007
    • 34

    store menu array data between forms

    My menu options provides hide/show checkbox areas on a form. During an IE postback or "back" the array is lost and the checkboxes disappear from view. Now I need a way to keep the array between pages so the added checkboxes on the form do not disappear.

    Thanks for your suggestions.
    [code=javascript]
    menu_status = new Array();
    function showHide(theid) {
    if (document.getEl ementById) {
    var switch_id = document.getEle mentById(theid) ;

    if(menu_status[theid] != 'show') {
    switch_id.class Name = 'show';
    menu_status[theid] = 'show';
    }else{
    switch_id.class Name = 'hide';
    menu_status[theid] = 'hide';
    }
    }
    }[/code]

    [code=html]
    <a class="a" href='#'>MainMe nu<br>
    <table>
    <tr><td><a href="#" onclick="showHi de('mymenu1')"> jump1</a></td></tr>
    <tr><td><a href="#" onclick="showHi de('mymenu2')"> jump2</a></td></tr>
    <tr><td><a href="#" onclick="showHi de('mymenu3')"> jump3</a></td></tr>

    </table>

    <div id="mymenu1" class=hide>
    <input type="checkbox" name="Topic" value="_select1 " /> jump1selection
    </div>

    <div id="mymenu2" class=hide>
    <input type="checkbox" name="Topic" value="_select2 " /> jump2selection
    </div>

    <div id="mymenu3" class=hide>
    <input type="checkbox" name="Topic" value="_select3 " /> jump3selection
    </div>
    [/code]
    Last edited by gits; Aug 20 '07, 09:37 PM. Reason: fix code tags
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Use cookies to store the current state of the checkboxes.

    Comment

    • swebster
      New Member
      • Jun 2007
      • 34

      #3
      Originally posted by acoder
      Use cookies to store the current state of the checkboxes.
      Thanks for the reply acoder.

      I have been looking into adding a cookie to store the menu status but it's still unclear how to do this properly. If the user selects and deselects multiple menu options during a single session does storing that information in a cookie really make sense? If so would I set a time out on the cookie?

      appreciate your feedback!

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Originally posted by swebster
        Thanks for the reply acoder.

        I have been looking into adding a cookie to store the menu status but it's still unclear how to do this properly. If the user selects and deselects multiple menu options during a single session does storing that information in a cookie really make sense? If so would I set a time out on the cookie?

        appreciate your feedback!
        You could do this just before leaving the page using onbeforeunload.

        Comment

        • swebster
          New Member
          • Jun 2007
          • 34

          #5
          Originally posted by acoder
          You could do this just before leaving the page using onbeforeunload.
          ok, so far I have included <body onbeforeunload= getcookie(); >
          added a document.cookie statement to show all menu options
          and have created the below function but could still use some help.

          [code=javascript]
          //....snip...
          if(menu_status[theid] != 'show') {
          switch_id.class Name = 'show';
          menu_status[theid] = 'show';
          document.cookie = menu_status(the id);
          //.....snip....

          function getcookie(menu_ status) {
          if (document.cooki e.length > 0)
          {
          menu_status=doc ument.cookie.in dexOf(menu_stat us + " ")
          if ( menu_status!= 1 )
          {
          menu_status=men u_startus + menu_status.len gth +1
          }
          }
          return " "
          }
          [/code]
          Last edited by gits; Aug 20 '07, 09:35 PM. Reason: fix code tags

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5388

            #6
            hi ...

            i fixed the code tags for you ... please use it the right way:

            Code:
            [ code=javascript ] ... [ /code ] - javascript
            [ html ] ... [ /html ] - html
            kind regards

            Comment

            • swebster
              New Member
              • Jun 2007
              • 34

              #7
              Originally posted by gits
              hi ...

              i fixed the code tags for you ... please use it the right way:

              Code:
              [ code=javascript ] ... [ /code ] - javascript
              [ html ] ... [ /html ] - html
              kind regards

              No problem. I'll be sure to use [code] comments on my future forum questions and replies. Thanks..

              I continue to have difficulty creating a cookie on menu select. Can anyone show me the way so I can expand my knowledge of this area?

              Much appreciated.

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                Originally posted by swebster
                I continue to have difficulty creating a cookie on menu select. Can anyone show me the way so I can expand my knowledge of this area?
                Read about getting, setting and deleting cookies in this article and this tutorial.

                If you use those functions, it will be easy to store the data.

                Comment

                • swebster
                  New Member
                  • Jun 2007
                  • 34

                  #9
                  Originally posted by acoder
                  Read about getting, setting and deleting cookies in this article and this tutorial.

                  If you use those functions, it will be easy to store the data.
                  Yes, w3schools.com is very informative. Thanks for the suggestions.

                  I am now able to store a cookie on the client machine and feel I am half way there. Since the menu can be selected multiple times before an actual submit I have decided the best place for the setCookie is on the submit. With this however, I have difficulty getting the menu_status into the cookie for redisplay.

                  Here is what I have tried.

                  [code=javascript]
                  menu_status = new Array();

                  function showHide(theid) {
                  if (document.getEl ementById) {
                  var switch_id = document.getEle mentById(theid) ;

                  if(menu_status[theid] != 'show') {
                  switch_id.class Name = 'show';
                  menu_status[theid] = 'show';

                  }else{
                  switch_id.class Name = 'hide';
                  menu_status[theid] = 'hide';
                  }
                  }
                  }

                  function setCookie(menuC ookie,value,day s) {
                  if (days) {
                  var date = new Date();
                  date.setTime(da te.getTime()+(d ays*24*60*60*10 00));
                  var expires = "; expires="+date. toGMTString();
                  }
                  else var expires = "";
                  document.cookie = menuCookie+"="+ value+expires+" ; path=/";
                  }

                  function getCookie(menuC ookie) {
                  var nameEQ = menuCookie + "=";
                  var ca = document.cookie .split(';');
                  for(var i=0;i < ca.length;i++) {
                  var c = ca[i];
                  while (c.charAt(0)==' ') c = c.substring(1,c .length);
                  if (c.indexOf(name EQ) == 0) return c.substring(nam eEQ.length,c.le ngth);
                  }
                  return null;
                  }

                  function eraseCookie(men uCookie) {
                  createCookie(me nuCookie,"",-1);
                  }
                  [/code]

                  [html]

                  <input type=submit value="Search" onclick="'valid query';setCooki e('menuCookie', '<%=request.get Parameter("menu _status()")%>', '7');" />

                  [/html]

                  This returns null in the cookie value.

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    You can only use request.getPara meter whilst the page is loading. After the page has loaded, to change anything, you will need Javascript. Did you mean to pass the array to the function? Try setting a cookie for each menu id. You could just loop over the array and set each one.

                    Comment

                    • swebster
                      New Member
                      • Jun 2007
                      • 34

                      #11
                      Originally posted by acoder
                      You can only use request.getPara meter whilst the page is loading. After the page has loaded, to change anything, you will need Javascript. Did you mean to pass the array to the function? Try setting a cookie for each menu id. You could just loop over the array and set each one.
                      By setting the cookie on each menu selection doesn't that create a lot more traffic to the client machine? A client could select menu option 1, and 2 but then decide menu option 3 is what they really want before submitting. Thus creating three cookie updates for the single transaction. Can I pass the menu array data to a hidden field and either pass that between the forms or instruct the setCookie function to use the menu array data onsubmit?

                      Comment

                      • swebster
                        New Member
                        • Jun 2007
                        • 34

                        #12
                        Acoder, I could use some of that expertise of yours.

                        I have update my code to set menu status_id in a cookie.
                        The js also removes the cookie when the menu option is
                        selected the second time( which in this case hides the display
                        of that menu ). What I need help with is getting the cookie back
                        and displaying the appropriate menu according to the cookie value.

                        I'm stuck so anything you can suggestion would
                        be greatly appreciated.

                        thanks

                        Code: ( text )
                        [code=javascript]
                        function showHide(theid) { // theid is the menu option selected

                        if (document.getEl ementById) {
                        var switch_id = document.getEle mentById(theid) ;

                        if(menu_status[theid] != 'show'){
                        switch_id.class Name = 'show';
                        menu_status[theid] = 'show';
                        setCookie('menu C',[theid],2); //this works

                        }else{
                        switch_id.class Name = 'hide';
                        menu_status[theid] = 'hide';
                        checkall('query Form','Topic',f alse);
                        eraseCookie('me nuC'); // distroy cookie
                        }
                        }
                        }

                        function setCookie(menuC ,value,days) {
                        if (days) {
                        var date = new Date();
                        date.setTime(da te.getTime()+(d ays*24*60*60*10 00));
                        var expires = "; expires="+date. toGMTString();
                        }
                        else
                        var expires = "";
                        document.cookie = menuC+"="+value +expires+"; path=/";
                        }

                        function getCookie(menuC ) {
                        var nameEQ = menuC + "=";
                        var ca = document.cookie .split(';');
                        for(var i=0;i < ca.length;i++) {
                        var c = ca[i];
                        while (c.charAt(0)==' ') c = c.substring(1,c .length);
                        if (c.indexOf(name EQ) == 0) return c.substring(nam eEQ.length,c.le ngth);
                        }

                        return null;
                        }

                        function eraseCookie(men uC) {
                        setCookie(menuC ,"",-1);
                        }

                        [/code] - javascript
                        Last edited by acoder; Sep 18 '07, 10:12 AM. Reason: fixed code tag

                        Comment

                        • acoder
                          Recognized Expert MVP
                          • Nov 2006
                          • 16032

                          #13
                          Originally posted by swebster
                          What I need help with is getting the cookie back and displaying the appropriate menu according to the cookie value.
                          I assume you would want this to happen when you revisit the page. Call a function which goes through each menu option and gets the cookie. If set, it shows the menu otherwise it is kept hidden.

                          Comment

                          • swebster
                            New Member
                            • Jun 2007
                            • 34

                            #14
                            Originally posted by acoder
                            I assume you would want this to happen when you revisit the page. Call a function which goes through each menu option and gets the cookie. If set, it shows the menu otherwise it is kept hidden.
                            Yes, upon pageback the menu option should still be visiable. I am able to get the cookie value back and alert it to the screen, however I am not able to find the correct syntax to unhide the div.class for the menu option selected.

                            Do you have any suggestions?

                            Code: (javascript)
                            [code=javascript]
                            function menu_redisplay (queryForm) {
                            var x = getCookie('menu C')
                            switch (x){
                            case "menu1":
                            class = 'show'
                            break;
                            case "menu2":
                            class = 'show'
                            break;
                            case "menu3":
                            class = 'show'
                            break;
                            case "menu4":
                            class = 'show'
                            break;
                            default:
                            alert("no menu in cookie")
                            }
                            }
                            [/code] - javascript

                            Comment

                            • swebster
                              New Member
                              • Jun 2007
                              • 34

                              #15
                              Originally posted by swebster
                              Yes, upon pageback the menu option should still be visiable. I am able to get the cookie value back and alert it to the screen, however I am not able to find the correct syntax to unhide the div.class for the menu option selected.

                              Do you have any suggestions?

                              Code: (javascript)
                              [code=javascript]
                              function menu_redisplay (queryForm) {
                              var x = getCookie('menu C')
                              switch (x){
                              case "menu1":
                              class = 'show'
                              break;
                              case "menu2":
                              class = 'show'
                              break;
                              case "menu3":
                              class = 'show'
                              break;
                              case "menu4":
                              class = 'show'
                              break;
                              default:
                              alert("no menu in cookie")
                              }
                              }
                              [/code] - javascript

                              Here is a menu cookie that works on single menu selections.
                              Next stage, build a cookie and js to support mulitple menu sections.


                              Code: (javascript)
                              [code=javascript]
                              function menu_redisplay (queryForm) {
                              var x = getCookie('menu C')
                              var switch_id = document.getEle mentById(x);
                              switch (x){
                              case "menu1":
                              switch_id.class Name = 'show'
                              break;
                              case "menu2":
                              switch_id.class Name = 'show'
                              break;
                              case "menu3":
                              switch_id.class Name = 'show'
                              break;
                              case "menu4":
                              switch_id.class Name = 'show'
                              break;
                              default
                              }
                              }
                              [/code] - javascript[/QUOTE]

                              Thanks.

                              Comment

                              Working...