Forum Moderators: coopster

Message Too Old, No Replies

Dynamic Drop Down Boxes

         

GeekGurl

3:56 am on Feb 15, 2005 (gmt 0)



Hello. I am very new to PHP but have quit a bit of JavaScript knowledge. I am trying to figure out how to simulate the onChange() function from JavaScript with PHP.

What I am wanting to accomplish is to generate a second drop down select menu based upon the option selected in a first select menu.

I know this can be done using $_POST or through data stored in my mySQL database. What I am looking for is a way to generate DROP DOWN 2 on-the-fly when the selection in DROP DOWN 1 is changed (like with the onChange() function available through JavaScript).

Any help or advise would be greatly appreciated. =)

grandpa

4:04 am on Feb 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Just off the top of my head... (often a dangerous place).

If you had an array loaded with values for your second drop down, then you could populate the second drop down using the value of the first drop down as the key to the array. It should work. But that begs the question of why php for this, since it seems to be a natural for js.

And, Welcome to WebmasterWorld.

mincklerstraat

8:51 am on Feb 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Most server-side languages like PHP aren't going to have something like OnChange() like js - with server-side stuff, think, 'once the page is made, that's it.' If you want to do fancy dynamic stuff on a page (allow it to change after it's rendered), most server-side languages will just have you use the server-side language itself to generate the appropriate javascript.

Basically, then, write your javascript, possibly even 'dynamically' (i.e., the javascript is can be different depending on the request), and then have PHP echo it out or process it for you.

dmmh

5:25 pm on Feb 15, 2005 (gmt 0)

10+ Year Member



minckler is spot on. I use someone's javascript for something exactly like you want, but I decided I couldnt be arsed to make the js file be written with PHP, just too much work, since the javascript is pretty damn complicated :)

Also have some other dynamical select menu's generated from DB queries and written with PHP, its not too hard tbh, but for what you want I couldnt begin to draw you an example :)