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

PHP

Drop down form PHP & mysql

How can i get the data from a drop down list and then submit it to the database. i know how to do the php and the mysql side but how does the drop down get the data to the mysql function? with also being multiple variables

Never mind i figure it out..

1 Answer

You can use a foreach statement in php when you have an array of options from a table in a database. For example:

<form action="pathto/script.php">
<select name="someOption">
<?php foreach ($options as $option):?>
<option value="<?php echo $option['name'];?>"><?php echo $option['value'];?></option>
<?php endforeach;?>
</select>
</form>

This will loop through the options and populate a select field in the form that you can then post to the db. I hope this is what you were looking for.