Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

General Discussion

auto populate fields from a drop down selection

Does anyone know how to create a form with a drop down selection and depending on what is selected having the form fields auto populate.

For instance, I have a form for customers where you can fill in the fields for the customer name, address, phone etc. When you get to the customers company field there is a drop-down selection- you can either select a current company and the contact details for that company will auto populate the rest of the form or you can select new company and enter company details.

Any ideas on how I would approach this?

4 Answers

Try something like this http://jsfiddle.net/zunrk/

Thanks Dave, this works well - but is there a way I can pull that company info from a MySQL database (sorry I should have mentioned above...... I'm still a bit of a newbie!)

By using the foreach() (used for arrays) function in PHP you can pull from a MySQL DB. So something like this would work:

foreach ($whatever as $whatever) {
    echo '<option>'.$your-company-variable.'</option>';
}

When you put that code inside the <select> tag, you will be able to dynamically pull data from a DB. For more information on foreach and more examples see http://us1.php.net/manual/en/control-structures.foreach.php

Thanks Dave - this is great!