Changing contents of a dropdown box

Discussion in 'PHP' started by lonewolff, Dec 21, 2009.

  1. #1
    Hi guys!

    Is there any way of changing the contents of a dropdown box due to what was selected in a previous box?

    For example;

    Contents of Box 1 might be 'Ram' or CPU's'

    Can we then change Box 2 dependent on the selection of Box 1 to show its own list?

    Thanks in advance :)
     
    lonewolff, Dec 21, 2009 IP
  2. Colbyt

    Colbyt Notable Member

    Messages:
    3,224
    Likes Received:
    185
    Best Answers:
    0
    Trophy Points:
    210
    #2
    With PHP and a page refresh I think you can. There may be an Ajax solution also but about that I know nothing.
     
    Colbyt, Dec 21, 2009 IP
  3. lonewolff

    lonewolff Member

    Messages:
    338
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    30
    #3
    So the end user would have to manually refresh would they?
     
    lonewolff, Dec 21, 2009 IP
  4. RevoBulletin

    RevoBulletin Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    No not necessarily. You could use Ajax to send a request to another page and update the next dropdown box. I'm no good with ajax as I haven't spent much time doing stuff like this. But... You can do a page refresh, which you can do a redirect which can be like this: page.php?box1=server1 and then you can have if statements that show only RAM and/or CPU for dropdown #2.
     
    RevoBulletin, Dec 21, 2009 IP
  5. Colbyt

    Colbyt Notable Member

    Messages:
    3,224
    Likes Received:
    185
    Best Answers:
    0
    Trophy Points:
    210
    #5
    No. The simple way to do it using PHP, assuming you are only going to have one or two branches is to make the process a series of steps where the previously entered information is passed as a $_post[variable] to the same or new page.

    Sorta like one of those online surveys.
     
    Colbyt, Dec 21, 2009 IP
  6. creativeGenius

    creativeGenius Well-Known Member

    Messages:
    273
    Likes Received:
    5
    Best Answers:
    1
    Trophy Points:
    120
    #6
    yep, this is very much in use these days, you can use php/ajax

    pseudocode will be

    user selects something on dropdown 1

    an ajax script executes, querying the database or a predetermined array of data using the value in dropdown 1 as filter

    dropdown 2 gets populated with the results
     
    creativeGenius, Dec 21, 2009 IP
  7. lonewolff

    lonewolff Member

    Messages:
    338
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    30
    #7
    Hmmm. I was hoping not to have to manually refresh the page or do a step by step approach.

    Maybe I'll have to look at making a small flash application or something.
     
    lonewolff, Dec 21, 2009 IP
  8. creativeGenius

    creativeGenius Well-Known Member

    Messages:
    273
    Likes Received:
    5
    Best Answers:
    1
    Trophy Points:
    120
    #8
    you dont need to do that, through ajax/php you can have a linked dropdown w/o any sort of refresh/reload

    http://remysharp.com/wp-content/uploads/2007/01/select.html
     
    creativeGenius, Dec 21, 2009 IP
  9. CodedCaffeine

    CodedCaffeine Peon

    Messages:
    130
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #9
    You're pretty much limited to Javascript, unless you want to go the flash approach.. haha. You could have the data loaded with AJAX techniques, or you could have all the data rendered at one time, then have everything as a hidden display (display:none). when someone selects a certain box, the js would enable the options you want to be shown, then remove when it's not needed. If you went with that approach, I'd highly recommend a cache system to be involved.

    Hope this helps. :)
     
    CodedCaffeine, Dec 21, 2009 IP
  10. lonewolff

    lonewolff Member

    Messages:
    338
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    30
    #10
    This looks promising. I'll see if I can make sense of the source.

    [edit]any idea where I can find the original post for this link?[/edit]
     
    Last edited: Dec 21, 2009
    lonewolff, Dec 21, 2009 IP