Hi I am following the code example on the following link :
http://www.huanix.com/files/dependent_select/dependent_select.php
The tables in the example database each is having an integer identifier and corresponding field. My MySQL table contains two fields (organism, genomeVersion) both varchar type. I want to make a drop down list that after selecting organism (first option), it query the 'organismGenomeVer' table and on the basis of that it shows me the option in the second field. I found many forums including the one above but i am not able to incorporate the above example into mine. The example is working correctly but it is not working with my data. I some how found the way to do it but I dont like the way I am doing. It seem that the option tag inside while loop only takes identifier. Can anyone please suggest me the correct way of doing it. My example below does not does show the selection for organism but it is not showing for genomeVersion.
Thanks a lot.
database table:
create table organismGenomeVer ( organism varchar(100), genomeVer varchar(100));
insert into organismGenomeVer ('human', 'hg19');
insert into organismGenomeVer ('human', 'hg18');
insert into organismGenomeVer values ('mouse', 'mm9');
insert into organismGenomeVer values ('mouse', 'mm8');
insert into organismGenomeVer values ('rat', 'rn4');
insert into organismGenomeVer values ('rat', 'rn3');
<?php
$organism = $genomeVer = null; //declare vars
$conn = mysql_connect('localhost', 'root', '****');
$db = mysql_select_db('test',$conn);
if(isset($_GET["organism"]) && is_numeric($_GET["organism"]))
{
$organism = $_GET["organism"];
}
if(isset($_GET["genomeVer"]) && is_numeric($_GET["genomeVer"]))
{
$genomeVer = $_GET["genomeVer"];
}
?>
<script language="JavaScript">
function autoSubmit()
{
var formObject = document.forms['theForm'];
formObject.submit();
}
</script>
<form name="theForm" method="get">
<!-- organism SELECTION -->
<select name="organism" onChange="autoSubmit();">
<option value="null">Select Organism</option>
<option value= "1" <?php if($organism == 1) echo " selected"; ?>>human</option>
<option value= "2" <?php if($organism == 2) echo " selected"; ?>>mouse</option>
<option value= "3" <?php if($organism == 3) echo " selected"; ?>>rat</option>
</select>
<br><br>
<!-- genomeVer SELECTION BASED ON organism VALUE -->
<?php
if ($organism == null) {
print "organism empty\n";
exit;
}
if($organism != null && is_numeric($organism))
{print "organism is $organism\n";
print "\n\n\norganism befor selecorg is $organism";
//POPULATE DROP DOWN MENU WITH genome version FROM A GIVEN organism
if ($organism == '1') { $selectedOrganism = 'human';}
elseif ($organism == '2') { $selectedOrganism = 'mouse';}
elseif ($organism == '3') { $selectedOrganism = 'rat';}
print "selected organism is $selectedOrganism\n\n\n\n";
?>
<br><br>
<?php
print "\n\nselected organism is $selectedOrganism\n\n\n\n\n\n\n";
$sql = "SELECT genomeVer FROM organismGenomeVer WHERE organism = \"$selectedOrganism\"";
//$sql = "SELECT genomeVer FROM organismGenomeVer";
$result = mysql_query($sql,$conn);
print "result is $result";
if (!$result) {
print "DB Error, could not list tables\n";
print 'MySQL Error: ' . mysql_error();
exit;
}
?>
<br><br>
<select name="genomeVer" onChange="autoSubmit();">
<option value="null">select GenomeVersion</br></option>
<?php
while($row = mysql_fetch_array($result))
{
$val = $row['genomeVer'];
$name= $row['genomeVer'];
echo "<option value=$val>$name";
}
?>
</select>
<?php
}
?>
<br><br>
</form>