jamesnkk Posted March 27, 2006 Share Posted March 27, 2006 Hi, can anyone show me, how can I populate data into the drop down menu.Example I want to display all the customer name in the drop down menu, so when the user selected the customer name, I could also capture the cust id together with the customer name into another table. Quote Link to comment https://forums.phpfreaks.com/topic/5942-populate-a-drop-down-menu/ Share on other sites More sharing options...
Levinax Posted March 27, 2006 Share Posted March 27, 2006 here is a function i wrote to query a database and populate a SELECT box based on the results of that query if thats what you're looking for. there are probably better ways of doing it, but i know this one works.one thing i should note, this POSTS the information, and keeps whatever you selected selected. you might want to modify it if thats not what you want to do. and i was using PostgreSQL, so you might need to change some things from there too.[code]function first_form($db, &$location){//Getting the data from Postgres table for first list box$quer1="SELECT DISTINCT location FROM info";$result1 = pg_query($db, $quer1);$rows1 = pg_num_rows($result1);$location = $_POST["loc"];//creat first formecho "<form method=POST name=f1 action=$PHP_SELF>";echo "<select name=loc onchange=\"Javascript:submit()\"><option value=\"\">Location</option>";for($i = 0; $i < $rows1; $i++){ $results1 = pg_fetch_array($result1, NULL, PGSQL_ASSOC); if($results1[location] == $location) { echo "<option selected value=$location>$location</option>"; } else { echo "<option value=\"$results1[location]\">$results1[location]</option>"; }}echo "</select>";}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/5942-populate-a-drop-down-menu/#findComment-21286 Share on other sites More sharing options...
jamesnkk Posted March 27, 2006 Author Share Posted March 27, 2006 [!--quoteo(post=358982:date=Mar 28 2006, 02:35 AM:name=Levinax)--][div class=\'quotetop\']QUOTE(Levinax @ Mar 28 2006, 02:35 AM) [snapback]358982[/snapback][/div][div class=\'quotemain\'][!--quotec--]here is a function i wrote to query a database and populate a SELECT box based on the results of that query if thats what you're looking for. there are probably better ways of doing it, but i know this one works.one thing i should note, this POSTS the information, and keeps whatever you selected selected. you might want to modify it if thats not what you want to do. and i was using PostgreSQL, so you might need to change some things from there too.[code]function first_form($db, &$location){//Getting the data from Postgres table for first list box$quer1="SELECT DISTINCT location FROM info";$result1 = pg_query($db, $quer1);$rows1 = pg_num_rows($result1);$location = $_POST["loc"];//creat first formecho "<form method=POST name=f1 action=$PHP_SELF>";echo "<select name=loc onchange=\"Javascript:submit()\"><option value=\"\">Location</option>";for($i = 0; $i < $rows1; $i++){ $results1 = pg_fetch_array($result1, NULL, PGSQL_ASSOC); if($results1[location] == $location) { echo "<option selected value=$location>$location</option>"; } else { echo "<option value=\"$results1[location]\">$results1[location]</option>"; }}echo "</select>";}[/code][/quote]Thank so much for your coding , I will try as I am using mysql and php Quote Link to comment https://forums.phpfreaks.com/topic/5942-populate-a-drop-down-menu/#findComment-21322 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.