[Solved] Dynamic Drop Down Menu & register_globals
Posted: Wed Aug 04, 2004 10:13 pm
G'day,
I'm writing a small PHP/MySQL solution for keeping track of our lab's primate DNA samples. This consists of 2 SQL tables: the primate DNA table (ie Gorilla gorilla) and the organization where we got it table (ie San Diego Zoo). We deal with a number of organizations that send us DNA from the same species, so we designed an 'add sample' form where when you enter in a new sample, a drop down menu is used to call the organization table. This saves having to type the name of organization and keeps consistency when you need to do a search for all samples sent by that organization. The drop down menu does not insert the name of the organization into the 'add sample' submit, rather it puts the 'id' value of the organisation. This is useful because if the organization changes its' name, you only have to change the name, but not the ids. Thus your samples will still refer to the id of the organization and won't need to be changed.
We also have an 'edit sample' form as well, for the inevitable case when someone enters in incorrect information. In this case, the information is already in the samples table and we just call it into the form. In the case of the organization drop down menu, the id is submitted to the organization table and the corresponding name is returned to the drop down menu. When you click submit, the id of the organization is submitted to the samples table.
Prior to register_globals being turned off by default, all of the code in the 'edit sample' form worked fine.
This example of the code shows the dynamic drop down menu from the 'edit sample' form in question. So say for example the DNA sample gorilla has an organization id of '1', then the menu will translate that to the corresponding organization name from the organization table. In my case organization id ='1' translates to 'San Diego Zoo'. PHP will then display the name of the organization in the drop down menu rather than the id.
However now with register_globals being turned off, the code stopped working. I've tried in my limited PHP knowledge to get it limping again. But this hasn't been 100% successful.
What is happening now is that the drop down menu does not match the organization 'id' field in the samples table to the corresponding organization name in the organization table. So say for example we have id '1' = San Diego Zoo and id '2' = Melbourne Zoo, if the DNA sample has an organization id of '1' instead of setting the menu to say 'San Diego Zoo', it will set the drop down menu to the first entry in the drop down menu, in this case the default option value of 'unspecified' [see code]. If I remove the option value of 'unspecified', then any org with an id of 2 will now say San Diego Zoo because it is now the first field on the menu.
As I'm a scientist and not a programmer, forgive the lameness of the code.
I've manualed, googled and forum searched this one to death, so any insights into what I'm mucking up would be greatly appreciated.
Thanks in advance,
Luke Kirkwood
I'm writing a small PHP/MySQL solution for keeping track of our lab's primate DNA samples. This consists of 2 SQL tables: the primate DNA table (ie Gorilla gorilla) and the organization where we got it table (ie San Diego Zoo). We deal with a number of organizations that send us DNA from the same species, so we designed an 'add sample' form where when you enter in a new sample, a drop down menu is used to call the organization table. This saves having to type the name of organization and keeps consistency when you need to do a search for all samples sent by that organization. The drop down menu does not insert the name of the organization into the 'add sample' submit, rather it puts the 'id' value of the organisation. This is useful because if the organization changes its' name, you only have to change the name, but not the ids. Thus your samples will still refer to the id of the organization and won't need to be changed.
We also have an 'edit sample' form as well, for the inevitable case when someone enters in incorrect information. In this case, the information is already in the samples table and we just call it into the form. In the case of the organization drop down menu, the id is submitted to the organization table and the corresponding name is returned to the drop down menu. When you click submit, the id of the organization is submitted to the samples table.
Prior to register_globals being turned off by default, all of the code in the 'edit sample' form worked fine.
This example of the code shows the dynamic drop down menu from the 'edit sample' form in question. So say for example the DNA sample gorilla has an organization id of '1', then the menu will translate that to the corresponding organization name from the organization table. In my case organization id ='1' translates to 'San Diego Zoo'. PHP will then display the name of the organization in the drop down menu rather than the id.
Code: Select all
<tr>
<td valign="top"><font size="-1" face="Verdana, Arial, Helvetica, sans-serif">Organization</font></td>
<td>
<select name="org">
<option value=""><unspecified></option>Code: Select all
<?php
// get organization list
$query = "SELECT id, name from organization ORDER BY id";
$result = mysql_db_query($db, $query, $connection) or die ("Error in query: $query. " . mysql_error());
while (list($id, $org) = mysql_fetch_row($result))
{
echo "<option value=$id";
if ($id == $name)
{
echo " selected";
}
echo ">$org</option>";
}
mysql_free_result($result);
?>Code: Select all
<?php
// get organization list
$query = "SELECT id, name from organization ORDER BY id";
$result = mysql_db_query($db, $query, $connection) or die ("Error in query: $query. " . mysql_error());
while (list($id, $org) = mysql_fetch_row($result))
{
echo "<option value=$id";
if ($_GET['id'] == $name)
{
echo " selected";
}
echo ">$org</option>";
}
mysql_free_result($result);
?>As I'm a scientist and not a programmer, forgive the lameness of the code.
I've manualed, googled and forum searched this one to death, so any insights into what I'm mucking up would be greatly appreciated.
Thanks in advance,
Luke Kirkwood