Page 1 of 1

[Solved] Dynamic Drop Down Menu & register_globals

Posted: Wed Aug 04, 2004 10:13 pm
by Architeuthis
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.

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);
?>
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.

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);
	
?>
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

Posted: Thu Aug 05, 2004 2:55 am
by LiquidPro
So your only problem is that you can't get it working with register_globals turned off I assume?

First of all you have to look at your <form> tag in the HTML. If this is set to GET then you can use the $_GET array for the variables in your code. If this is set to POST you must use the $_POST array for your variables.

Example:

Code: Select all

&lt;form method="post" action="send_data.php"&gt;
&lt;input type="text" name="name" value="LiquidPro"&gt;
&lt;/form&gt;
If you use that, you must use this as your code:

Code: Select all

if($_POST['name'] == "LiquidPro") {
   echo "You are from Akron, OH!";
} else {
   echo "I don't know where you are from, sorry!";
}
Help any?

Posted: Thu Aug 05, 2004 3:27 am
by Architeuthis
G'day LiquidPro,

Yes, the problem is only occuring with register_globals turned off. You were also right about the <form> tag, I was using POST - doh. However changing from GET to POST reversed the problem. So now instead of the drop down menu just defaulting on the first item in the menu, it now defaults to the last item in the menu.

ie Using the 'Unspecified' default, 'San Diego Zoo' = 1, 'Melbourne Zoo' = 2 example again, using GET - the menu defaults to 'Unspecified', using POST - the menu defaults to 'Melbourne Zoo'. Tres Bizarre.

Here is the new code based on LiquidPro's suggestion.

Code: Select all

<?php
  	// get title 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 ($_POST['id'] == $name)
			{
			echo " selected";
			}
	
		echo ">$org</option>"; 	
		}
	mysql_free_result($result);
	

?>
Any ideas why this is happening?

Luke

Posted: Thu Aug 05, 2004 10:07 am
by LiquidPro
Wait... I just noticed something. Your $name variable, is nothing. You need to change your if condition to...

Code: Select all

if($_POST['id'] == $id)

Posted: Thu Aug 05, 2004 6:56 pm
by Architeuthis
G'day LiquidPro,

I am a freakin idiot. Your post sparked a bit of the grey matter and made me go back over my SQL fields, and turns out I had made a change but I didn't follow through on it. Thus I've been using two different naming structures in my SQL. No wonder the damn thing couldn't display the correct data. Maybe I need a tattoo on my hand which says 'DOCUMENT YOUR CODE STRAIGHT AWAY'. Maybe both hands.

The corrected code is

Code: Select all

<?php
	// get title list
	$query = "SELECT id, organization 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 == $organization)
			{
			echo " selected";
			}
	
		echo ">$org</option>"; 	
		}
	mysql_free_result($result);
?>
Thanks a bunch anyway LiquidPro. Without having someone to talk the code through, I doubt I would've ever noticed it. I'll mark this as solved.

Luke Kirkwood