Jump to content

Recommended Posts

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...

 

Link to comment
https://forums.phpfreaks.com/topic/97190-list-menu-problem/
Share on other sites

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>

Link to comment
https://forums.phpfreaks.com/topic/97190-list-menu-problem/#findComment-497531
Share on other sites

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..

Link to comment
https://forums.phpfreaks.com/topic/97190-list-menu-problem/#findComment-498304
Share on other sites

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)?

Link to comment
https://forums.phpfreaks.com/topic/97190-list-menu-problem/#findComment-499394
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.