mimi Posted March 21, 2008 Share Posted March 21, 2008 I'm having difficulties in list menu. For example, i have 2 list menu and a list of subjects, so once a subject (eg: subject XYZ) has been selected in list menu 1, the selected subject (subject XYZ) will not display in the list menu 2. is there anyone know how to solve this prob?? please help me... Quote Link to comment https://forums.phpfreaks.com/topic/97190-list-menu-problem/ Share on other sites More sharing options...
rhodesa Posted March 21, 2008 Share Posted March 21, 2008 How about this: <html> <head> <script type="text/javascript"> var subjects = { 's1': 'Subject 1', 's2': 'Subject 2', 's3': 'Subject 3', 's4': 'Subject 4', 's5': 'Subject 5', }; function setLists ( listNodeIds ) { if(!listNodeIds.length) return false; var nodes = {}; var values = {}; var used = {}; for(var i=0;i < listNodeIds.length;i++){ var id = listNodeIds[i]; nodes[id] = document.getElementById(id); //Save current value values[id] = nodes[id].value; if(values[id]) used[values[id]] = 1; //Clear list nodes[id].options.length = 0; //Add first option var firstOption = document.createElement('option'); firstOption.text = '--Select a subject--'; nodes[id].add(firstOption,null); } for(var key in subjects){ for(var id in nodes){ var opt = document.createElement('option'); opt.value = key; opt.text = subjects[key]; if(values[id] == key) opt.selected = true; else if(used[key]) continue; nodes[id].add(opt,null); } } } </script> </head> <body onload="setLists(['list1','list2']);"> <select id="list1" onchange="setLists(['list1','list2']);"></select> <select id="list2" onchange="setLists(['list1','list2']);"></select> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/97190-list-menu-problem/#findComment-497531 Share on other sites More sharing options...
mimi Posted March 22, 2008 Author Share Posted March 22, 2008 but the list of menu is from the database.. do u know how??? Quote Link to comment https://forums.phpfreaks.com/topic/97190-list-menu-problem/#findComment-498066 Share on other sites More sharing options...
Adam Posted March 22, 2008 Share Posted March 22, 2008 what happens after they've switched menus? they supposed to stay there even when page has been refreshed? You need to look into arrays for storing the lists.. for getting them out of the database in the first place you'll be needing to learn a bit of server side.. PHP for example.. query the database and then just output them in into the javascript its self.. Quote Link to comment https://forums.phpfreaks.com/topic/97190-list-menu-problem/#findComment-498304 Share on other sites More sharing options...
rhodesa Posted March 24, 2008 Share Posted March 24, 2008 but the list of menu is from the database.. do u know how??? This is easy enough to do, but there are a couple options. It will require a server-side language (like PHP), so first, answer a couple questions please: 1) Does the example I posted a earlier function like you want? It will be easier to make changes to that before the DB integration. 2) Will PHP be the server-side language of choice? 3) Do you know PHP well enough to interact with a database? 4) How many "Subjects" will there be (estimate)? Quote Link to comment https://forums.phpfreaks.com/topic/97190-list-menu-problem/#findComment-499394 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.