Jump to content

php form select list


bdrever
 Share

Recommended Posts

Hi guys,I would like to create a form that pulls data from a database and displays the values it pulls as elements in a select style dropdown list. Any tips on how to do this?Example:The first dropdown box the user selects a type of food: PizzaThe next dropdown box automatically fills with different kinds of pizza i.e pepperoni, cheese, etc..

Link to comment
Share on other sites

Look in the PHP or Javascript forums, that question gets answered several times per week it seems like.
Thanks for the heads up!I've managed to create a form that will populate the second dropdown list, based on the user input of the first, but only AFTER the submit button has been pressed. How can I make it do it upon changing the information in the first box?I thought of using the javascript onChange event handler but I am not sure how to pass the value from js into php which I need to do in order to query my db..Any ideas? I was dreaming about this lastnight and thought I had a solution but I doesn't work.
<?php$brand = $_POST['CarMfg'];$query = "SELECT mfg, model FROM test.cars WHERE mfg = '$brand'";$result = mysql_query($query);?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><title>Untitled Document</title></head><body><form name ="test" method="post" action="ListTest.php">phone : <select name="CarMfg" onchange="update()">	<option value="" selected><?php print $brand; ?></option>	<option value ="Ford">Ford</option>	<option value ="Dodge">Dodge</option>	<option value ="GMC">GMC</option>	</select><br>	<br>model: <select name"model"><?php	while ($row = mysql_fetch_assoc($result)) 	{ 		print '<option value="'; 		print $row['model'];		print '">';		print $row['model'];		print '</option>';	}?></select><br><br><input type="submit" value="submit"></form></body></html>

Link to comment
Share on other sites

If you want to use Javascript, you need to have PHP write out the entire database at once. So PHP would create a big array in Javascript of all the possible values. The other way is to use AJAX to have Javascript send a request to a PHP page on the server, which would get the data and send it back to update the page. It's a little more complex, and a little heavier on the database, but it works well. If you are interested in that, do a Google search for AJAX to learn more.

Link to comment
Share on other sites

If you want to use Javascript, you need to have PHP write out the entire database at once. So PHP would create a big array in Javascript of all the possible values. The other way is to use AJAX to have Javascript send a request to a PHP page on the server, which would get the data and send it back to update the page. It's a little more complex, and a little heavier on the database, but it works well. If you are interested in that, do a Google search for AJAX to learn more.
thanks for pointing me in the right direction. I really did not know where to begin
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...