SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Git-R-Done
    Join Date
    Nov 2001
    Posts
    1,194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need help with two select menus (second menu is for sub-categories)

    I have a form that has two select menus. The first one is for the main categories and the second is for sub-categories. An example would be if the first menu listed car manufacturers then the sub-category select menu listed vehicle names. Can someone point me in the right direction or link me to a tutorial on how to get the second menu to display the vehicles that are listed under the selected car manufacturer? I've searched all over and cannot find info on how to do this. The data that is in each of the select boxes are in seperate tables. I guess JavaScrpt would be involved with the switching of the second select menu.
    John Saunders

  2. #2
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What you would have to do, is read all the values, and dynamically generate Javascript arrays to hold this data (for each manufacturer). Then use onClick method, to select which array to use, and populate the select box. Sorry i can't be more specific, javascript isn't my thing.

    -Peter

  3. #3
    SitePoint Addict
    Join Date
    Jan 2004
    Location
    Colorado Springs
    Posts
    308
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can steal my javascript at http://www.discreteautos.net/

    Look at the JavaScript at the bottom. If you know JavaScript, it should be hard to manipulate it.

    I used PHP to generate the arrays that are parsed from a CSV file. But any method works. Static/Dynamic as long as it parsed server-side.

  4. #4
    SitePoint Wizard swdev's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There are two scenarios that I can think of.
    First, as proposed above, use PHP to create the JavaScript that will handle the onchange event of the drop-down boxes. This will be okay as long as the user hasn't disabled JavaScript
    Second a total PHP solution - ie. whenever the user changes a list box, it requests the sub-category data from the server. This will work in all cases, but has the major disadvantge of needing to requrest the data from the server each time the user changes the list box.
    You pays your money and takes your choice.

  5. #5
    SitePoint Member
    Join Date
    Jan 2005
    Location
    ****
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am currently trying to implement the second method you mention, but i am a newbie for PHP, so you can tell me more details of the method.thanks

    Quote Originally Posted by swdev
    There are two scenarios that I can think of.
    First, as proposed above, use PHP to create the JavaScript that will handle the onchange event of the drop-down boxes. This will be okay as long as the user hasn't disabled JavaScript
    Second a total PHP solution - ie. whenever the user changes a list box, it requests the sub-category data from the server. This will work in all cases, but has the major disadvantge of needing to requrest the data from the server each time the user changes the list box.
    You pays your money and takes your choice.

  6. #6
    SitePoint Wizard swdev's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is just an of how I would go about doing it.

    Code:
      
      // define some constants
      define ('INITIAL', 0);
      define ('SET_MAIN_CATEGORY', 1);
      define ('SET_SUB_CATEGORY', 2);
      define ('FAILED_VALIDATION', 3);
      define ('ALL_OK', 4);
      
      $state = INITIAL;
      
      
      // work out how we got here
      if (submit buton was pressed)
      {
        if (main category has not been selected)
        {
      	$state = SET_MAIN_CATEGORY;
        }
        else
        {
      	if (sub category has not been selected)
      	{
      	  $state = SET_SUB_CATEGORY;
      	}
      	else
      	{
      	  if (all fields pass validation)
      	  {
      		$state = ALL_OK;
      	  }
      	  else
      	  {
      		$state = FAILED_VALIDATION;
      	  }
      	}
        }
      }
      
      // now build the appropriate drop-downs
      switch ($state)
      {
        case INITIAL:
      	build main category list with no selection
      	build empty sub category list with no selection
      	break;
      
        case SET_MAIN_CATEGORY:
      	build main category list with no selection
      	build empty sub category list with no selection
      	break;
      
        case SET_SUB_CATEGORY:
      	build main category list with selection = posted main cateogry
      	build sub category list with no selection based on posted main category
      	break;
      
        case FAILED_VALIDATION:
      	build main category list with selection = posted main cateogry
      	build sub category list with selection = posted sub category, based on posted main category
      	break;
      
        case ALL_OK:
      	redirect user to new page
      	break;
      
        default:
      	// some unkonwn state - so do the same as for an initial state, just to be on the safe side
      	build main category list with no selection
      	build empty sub category list with no selection
      	break;
      }
      
      build and display form here, using any data available from the posted variables
    Hope this gives you a starting point


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
  •