Jump to content

Archived

This topic is now archived and is closed to further replies.

jamesnkk

POPULATE A DROP DOWN MENU

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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 form
echo "<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]

Share this post


Link to post
Share on other sites
[!--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 form
echo "<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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.