Drop-down list dependency

Discussion in 'PHP' started by Lotos1, Feb 26, 2010.

  1. #1
    I have 2 drop-down lists using <select>. I want to be able to choose a value from the 1st drop-down list and then automatically populate the 2nd drop-down list based on the value selected in the 1st list. How can I do this using PHP and/or JavaScript?
     
    Last edited: Feb 26, 2010
    Lotos1, Feb 26, 2010 IP
  2. killaklown

    killaklown Well-Known Member

    Messages:
    2,666
    Likes Received:
    87
    Best Answers:
    0
    Trophy Points:
    165
    #2
    It can be done either with ajax, or creating arrays in javascript for each 'main' value with the values for the 2nd.. IE:

    var secondArray = new Array();

    secondArray['item1'] = new Array('<?php echo substr(implode("','",$array['item1'],0,-2)); ?>');
    secondArray['item2'] = new Array('<?php echo substr(implode("','",$array['item2'],0,-2)); ?>');
    secondArray['item3'] = new Array('<?php echo substr(implode("','",$array['item3'],0,-2)); ?>');


    Then when the value in the 1st select is changed, use DOM to create the options in the 2nd array for each of the values in the appropriate array.




    (would be easier, and better to use ajax, theres alot of scripts available if you search google for ajax dropdown)
     
    killaklown, Feb 26, 2010 IP
  3. Lotos1

    Lotos1 Peon

    Messages:
    454
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Do you have a link for the Ajax drop-down lists solution that works? I searched on Google and tried 2 different solutions and both don't do what I need. I need to somehow send a call to the server without refreshing the page and then process which value from the 1st drop-down list was selected, and based on that, populate the 2nd drop-down list without doing a complete page load.
     
    Lotos1, Feb 26, 2010 IP
  4. killaklown

    killaklown Well-Known Member

    Messages:
    2,666
    Likes Received:
    87
    Best Answers:
    0
    Trophy Points:
    165
    #4
    killaklown, Feb 27, 2010 IP
  5. Lotos1

    Lotos1 Peon

    Messages:
    454
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Lotos1, Feb 27, 2010 IP