Dynamicly update secondry box from selection.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
scorpio90
Forum Newbie
Posts: 23
Joined: Wed Mar 10, 2010 10:45 am
Location: Newcastle

Dynamicly update secondry box from selection.

Post by scorpio90 »

Hi guys,

Does anyone know of a java or php code that..

At the moment i have a dynmic option box, but when selected could update into a secondry option box.

For example,
If i was filling out a form and i had a option to chose my country .. say for example England.. then the secondary box will automatically populate with all the counties in England which then i could select in the secondary option box.

Any help would be great :D Thankyou.
katierosy
Forum Commoner
Posts: 27
Joined: Wed Apr 07, 2010 8:39 am

Re: Dynamicly update secondry box from selection.

Post by katierosy »

Please see jquery.com library , and it's ajax call methods in it's tutorials.
If you will call second select box from another file say x.php with <?php echo $var = '<options value="helo">Helo</options><options value="helo1">Helo1</options><options value="helo2">Helo2</options>'; ?> for example.

You will accomplish, what is needed.
User avatar
hypedupdawg
Forum Commoner
Posts: 74
Joined: Sat Apr 10, 2010 5:21 am

Re: Dynamicly update secondry box from selection.

Post by hypedupdawg »

Yes - you wont be able to do this using PHP as all PHP code is executed on the server-side. To use a php script to re-populate the boxes, you would have to refresh the page each time, using a $_POST or $_GET method. While this is something a lot of forms seem to do, it is much better for you to use a client-side method to do this (e.g. the javascript innerHTML property).
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Dynamicly update secondry box from selection.

Post by Jonah Bron »

First of all, Java is not Javascript. They are completely different. I know that doesn't make any sense, but that's how it is.

Like the previous posters stated, you will need to call to get the countries/counties with AJAX. Have a file called "countries.txt", with all of the countries you want listed. Then, create a folder called "countries", and populate it with files like "england.txt", "poland.txt", "yugoslavia.txt", etc. Each of those text files will contain the counties of their respective countries. You will use Javascript/AJAX to call the right country file, depending on the user's country selection (the onchange event of the country selection box), and fill up the county selection box with the results.

Using Mootools or jQuery make the AJAX much easier.
Post Reply