Page 1 of 1

Populating an existing drop down list

Posted: Thu Oct 14, 2010 3:55 am
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.
:?

Re: Populating an existing drop down list

Posted: Thu Oct 14, 2010 5:42 am
by Mince
echo " <select name = \"lstBuildings\" id=\"lstBuildings\">"; //this is the existing DDL (drop down list)
There is no items in the list here?

Re: Populating an existing drop down list

Posted: Thu Oct 14, 2010 6:31 am
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.

Re: Populating an existing drop down list

Posted: Thu Oct 14, 2010 6:44 am
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>

Re: Populating an existing drop down list

Posted: Thu Oct 14, 2010 6:47 am
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!

Re: Populating an existing drop down list

Posted: Thu Oct 14, 2010 6:53 am
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>

Re: Populating an existing drop down list

Posted: Thu Oct 14, 2010 7:52 am
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.