Jump to content
Larry Ullman's Book Forums

Recommended Posts

I realise this is a bit bare bones but I have a form with a list-box of counties. I have another box to enter towns so if I select say Yorkshire I need to add and post towns within Yorkshire to a table and be able to link them within the database.

 

The end result I was looking for was if a user selects Yorkshire it will display all the towns in Yorkshire in another box. The user can then select the Town. (I know how to display the information I want the user to see from there)

 

In plain English a cascading combo box (I think)

 

Not sure if I have explained it right.

Link to comment
Share on other sites

So basically, you want two drop-down lists, where the second one is dynamically populated based on the selection in the first one, correct?

If you want to do that without reloading the page, you will need to use JavaScript, and maybe Ajax. If you don't mind reloading the page, then you can do it all with just PHP.

 

Shall I explain more, or is that enough?

  • Upvote 1
Link to comment
Share on other sites

Again thanks for the nudge in the right direction. If anyone is wanting to create this please use the code below and just expand it.

<script type="text/javascript">
function fillForthCombo()
{
   var combo3 = document.getElementById('county');
   var combo4 = document.getElementById('town');
   var selected = combo3.options[combo3.options.selectedIndex].value;
   if (selected == "Avon")
   {
      combo4.options.length = 3;
      combo4.options[0] = new Option("Bath", "Bath");
      combo4.options[1] = new Option("Bradley Stoke", "Bradley Stoke");
      combo4.options[2] = new Option("Bristol", "Bristol");
      combo4.options[3] = new Option("Clevedon", "Clevedon");
      combo4.options[4] = new Option("Keynsham", "Keynsham");
      combo4.options[5] = new Option("Nailsea", "Nailsea");
      combo4.options[6] = new Option("Portishead", "Portishead");
      combo4.options[7] = new Option("Thornbury", "Thornbury");
      combo4.options[8] = new Option("Weston super Mare", "Weston super Mare");
      combo4.options[9] = new Option("Yate", "Yate");
      
   }
   else
   if (selected == "Bedfordshire")
   {
      combo4.options.length = 3;
      combo4.options[0] = new Option("Ampthill", "Ampthill");
      combo4.options[1] = new Option("Bedford", "Bedford");
      combo4.options[2] = new Option("Biggleswade", "Biggleswade");
      combo4.options[3] = new Option("Dunstable", "Dunstable");
      combo4.options[4] = new Option("Flitwick", "Flitwick");
      combo4.options[5] = new Option("Kempston", "Kempston");
      combo4.options[6] = new Option("Leighton Buzzard", "Leighton Buzzard");
      combo4.options[7] = new Option("Luton", "Luton");
      combo4.options[8] = new Option("Potton", "Potton");
      combo4.options[9] = new Option("Sandy", "Sandy");
      combo4.options[10] = new Option("Woburn", "Woburn");        
  }
Link to comment
Share on other sites

 Share

×
×
  • Create New...