Jump to content

Dynamic Select Menus With Php/ajax


skaterdav85
 Share

Recommended Posts

I was wondering what the best way is to create dynamic <select> menus. Right now i have 2 menus. The 1st menu has a list of Companies, and the 2nd menu has a list of buildings. The relationship in the database is 1-many. When a user changes the selection in the 1st menu (Companies menu), it makes an ajax call and sets the innerHTML of that 2nd menu (Buildings menu) based on the selection of the 1st menu. That works fine, but when i submit my search, and then press the Back button, the 1st menu still contains the selection and the 2nd menu contains all the buildings in the database, thus ignoring the criteria from the first menu. Is there a better way to create dynamic select menus? My only thought is to just have the ajax call reproduce the 2nd select menu (Buildings) instead of just the <option> tags that go inside it. However, by doing this, the user will see a little flash as the new select menu from the ajax call replaces the old one. Any ideas?

Link to comment
Share on other sites

I see. I was using safari but i havent tried it out in another browser. I guess the cache doesnt save what happened in the ajax call, so the 2nd list shows the default, which is all Buildings as if it were the same as when the page loaded and no Company selection was made. I do notice that when the ajax call is made, when you view the source of the page, the resulting <option> tags arent in the updated select menu. Do you know why that is?

Link to comment
Share on other sites

You're looking at the HTML source, Javascript doesn't change that, the HTML is the HTML. Javascript updates the document object model directly, not the source code. There are some tools that let you view the generated source after Javascript modifications, which it recreates from the object model.

Link to comment
Share on other sites

It's probably just loading the page from the cache. Different browsers do different things, some will try to restore the state of the page like it was when you left (e.g. form elements filled out), some will just load the page from cache.
what is saved in the cache? Every page when its first loaded in the browser?
Link to comment
Share on other sites

The response from the server is saved in the cache. When a browser requests a file it downloads it and saves it to the cache, then opens the local copy. So whatever is in the cache is whatever the server sent to the browser. The browser doesn't change files in the cache if Javascript modifies something.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...