Populating an existing drop down list

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
alexia_net
Forum Newbie
Posts: 17
Joined: Fri Oct 01, 2010 7:17 am

Populating an existing drop down list

Post by alexia_net »

Hi. I would like to know how can I select an existing drop down list from a form and populate it with the records from a database. I have written the code, but i do not know how to select the existing drop down list. It always creates a new one. This is the code I am using:

Code: Select all

$str_sql = "SELECT ......";
$result = mysql_query($str_sql) or ($slq_str . mysql_error());
	echo " <select name = \"lstBuildings\" id=\"lstBuildings\">"; //this is the existing DDL (drop down list)
	while ($row = mysql_fetch_array($result)){
		echo "<option value = $row[field]>$row[field]</option>";
	}
	echo "</select>";
}

But it creates a new list instead of populating the existing one. Thank you.
:?
Mince
Forum Commoner
Posts: 25
Joined: Mon Aug 03, 2009 9:36 am

Re: Populating an existing drop down list

Post by Mince »

echo " <select name = \"lstBuildings\" id=\"lstBuildings\">"; //this is the existing DDL (drop down list)
There is no items in the list here?
alexia_net
Forum Newbie
Posts: 17
Joined: Fri Oct 01, 2010 7:17 am

Re: Populating an existing drop down list

Post by alexia_net »

Hi. The list gets populated, but the program creates a new one, it is not using the one which already exists on the form. So the original one is still empty. Thank you.
Mince
Forum Commoner
Posts: 25
Joined: Mon Aug 03, 2009 9:36 am

Re: Populating an existing drop down list

Post by Mince »

Are you using the name of the drop down list twice, example:

Code: Select all

<select name = "lstBuildings" id="lstBuildings">
    <option value="1">hello</option>
    <option value="2">bye</option>
  </select>
<?php
// and then try populate it with more information here:
$str_sql = "SELECT ......";
$result = mysql_query($str_sql) or ($slq_str . mysql_error());
        echo " <select name = \"lstBuildings\" id=\"lstBuildings\">";
        while ($row = mysql_fetch_array($result)){
                echo "<option value = $row[field]>$row[field]</option>";
        }
        echo "</select>";
}
?>
if this is the case, the first one is being overwritten. rather use:

Code: Select all

<select name = "lstBuildings" id="lstBuildings">
    <option value="1">hello</option>
    <option value="2">bye</option>
<?php
$str_sql = "SELECT ......";
$result = mysql_query($str_sql) or ($slq_str . mysql_error());
        while ($row = mysql_fetch_array($result)){
                echo "<option value = $row[field]>$row[field]</option>";
        }
?>
  </select>
alexia_net
Forum Newbie
Posts: 17
Joined: Fri Oct 01, 2010 7:17 am

Re: Populating an existing drop down list

Post by alexia_net »

Hi. Yes. Once when defining the form for input, and once when trying to populate it. The code for populating the list is in a different file however, but this should not be a problem, the action gets executed on the same page. If I am going to use the name only once, how can I place it into a specific cell of the table on the form? Thank you!
Mince
Forum Commoner
Posts: 25
Joined: Mon Aug 03, 2009 9:36 am

Re: Populating an existing drop down list

Post by Mince »

Well that would not work, as soon as you use the <select name...> etc, it'll create a new one, or overwrite the existing one. Well if it's on two different pages, store the options in a variable, and echo it later? like this:

Code: Select all

<?php
$options = "";
$str_sql = "SELECT ......";
$result = mysql_query($str_sql) or ($slq_str . mysql_error());
        while ($row = mysql_fetch_array($result)){
                $options .= "<option value = $row[field]>$row[field]</option>";
        }
?>
<select name = "lstBuildings" id="lstBuildings">
    <option value="1">hello</option>
    <option value="2">bye</option>
<?php
echo $options;
?>
  </select>
alexia_net
Forum Newbie
Posts: 17
Joined: Fri Oct 01, 2010 7:17 am

Re: Populating an existing drop down list

Post by alexia_net »

Hi. I think its the same thing. It will still create a new list. Because the original list already exists. I forgot to mention that the list should get populated when I click a radio button. So my intention is not to create a new list, but to populate an existing one. Thank you.
Post Reply