SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    PHP Otaku Gibb's Avatar
    Join Date
    Jul 2004
    Location
    Texas
    Posts
    454
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    JavaScript to "Show" part of form resets with BACK button.

    This is hard to explain but here's my predicament. I have 2 drop down menus in a form for "Main Category" and "Restaurant Type". My boss wants it so when Restaurant is chosen from the first drop down, the 2nd drop down appears, which is what this code below is already doing. The problem is if you submit the form, then decide to go back and change your Restaurant type, the drop down for Restaurant Type is missing. You can get it to come back by switching to another main category, then switching back to Restaurant.

    Does anybody know how I could modify this code to fix this?

    Code:
    // in script section
    function toggleVisibleOnEquals(id, x1, x2)
    {	
    	var el = document.getElementById(id);
    	if (x1 == x2) 
    	{
    		el.style.visibility = "visible";		
    	}
    	else
    	{
    		el.style.visibility = "hidden";
    	}
    }
    
    <!-- and in the main code -->
    
    <select name="dt" style="width: 170px;" onChange="toggleVisibleOnEquals('foodtype',this.value,3016);" >
    <option>...
    </select>
    
    <span id="foodtype" style="visibility: hidden;">
    <select name="rt" style="width: 170px;" >
    <option>...
    </select>
    </span>
    Thanks in advance for any assistance.
    -Gibb

  2. #2
    SitePoint Addict jtrelfa's Avatar
    Join Date
    Oct 2004
    Location
    Troy, Mi
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is the toughest thing to deal with when it comes to DHTML 'stuff' - maintaining state. I'd suggest one of a couple solutions:
    1. Use a cookie - whenever the user makes a selection change or when the page is posted, set the cookie to remember what the page looked like
    2. use your servers Session handler to remember the state of the dropdowns. When the page is loaded, use your server-side code to pre-manipulate the page

    Those are the two I've used in the past and have *mostly* worked. In one particular case, I had to use an XHR to set/get DOM state from a database.


    HTH


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •