Copy link to clipboard
Copied
Hi,
I've been struggling over the last few days on a dropdown issue. I have a relationship between email table and address table and country table:
email:
idkey
name
address
address
idkey
address1
address2
address3
address4
postcode
fk_country_idkey
country
idkey
value
i have an onchange event on the email, so when i change the email address, all the details underneath change- address details. but the country is a dropdown list - i cannot get that to change to the correct item,
here is the code:
<form name="formcheck" ACTION="./Addressup.php" method="post">
<table width="560" border="0" align="center" cellspacing="0" cellpadding="0">
<tr>
<td width="40" height="37" class="topleft"></td>
<td colspan="5" class="top"></td>
<td width="40" height="37" class="topright"></td>
</tr>
<tr>
<td cellspacing="3" cellpadding="3" class="left"></td>
<td class="BoldItemText"> </td>
<td class="BoldItemText"> </td>
<td class="BoldItemText"> </td>
<td class="BoldItemText"> </td>
<td class="BoldItemText"> </td>
<td cellspacing="3" cellpadding="3" class="right"></td>
</tr>
<tr>
<td cellspacing="3" cellpadding="3" class="left"></td>
<td class="BoldItemText"> </td>
<td class="BoldItemText" align="right">Email</td>
<td class="BoldItemText"> </td>
<td class="ItemText" align="left">
<select name="select2" size="1" onchange="document.getElementById('add1').value = this.options[this.selectedIndex].getAttribute('altvalue1');
document.getElementById('add2').value = this.options[this.selectedIndex].getAttribute('altvalue2');
document.getElementById('add3').value = this.options[this.selectedIndex].getAttribute('altvalue3');
document.getElementById('add4').value = this.options[this.selectedIndex].getAttribute('altvalue4');
document.getElementById('value1').value = this.options[this.selectedIndex].getAttribute('altvalue0');
document.getElementById('postcode').value = this.options[this.selectedIndex].getAttribute('altvalue5');
document.formcheck.select3.selectedIndex = this.options[this.selectedIndex].getAttribute('altvalue6');">
<option value="#"><b>Please Select...</b></option>
<?php
$result3 = mysql_query("SELECT * FROM Email WHERE idkey IN (SELECT fk_email_idkey FROM Address) ORDER BY address" )
or die(mysql_error());
while($row3 = mysql_fetch_array( $result3 ))
{
$idk = $row3['idkey'];
$result4 = mysql_query("SELECT * FROM Address WHERE fk_email_idkey = '$idk'" )
or die(mysql_error());
while($row4 = mysql_fetch_array( $result4 ))
{
$idkey_country=$row4['fk_country_idkey'];
$result5 = mysql_query("SELECT * FROM Country WHERE idkey = '$idkey_country'" )
or die(mysql_error());
while($row5 = mysql_fetch_array( $result5 ))
{
echo "<option value=\"";
echo $row3['idkey'];
echo "\" altvalue0=\"";
echo $row3['idkey'];
echo "\" altvalue1=\"";
echo $row4['address1'];
echo "\" altvalue2=\"";
echo $row4['address2'];
echo "\" altvalue3=\"";
echo $row4['address3'];
echo "\" altvalue4=\"";
echo $row4['address4'];
echo "\" altvalue5=\"";
echo $row4['postcode'];
echo "\" altvalue6=\"";
echo $row5['idkey'];
echo "\" id=\"";
echo "test";
echo "\"> ";
echo $row3['address'];
echo "</option>\n";
}
}
}
$idk=0;
echo "</select>\n";
?>
</td>
<td class="BoldItemText">Idkey: <input type="text" id="value1" size="4" value="<?php echo $idk; ?>" /></td>
<td cellspacing="3" cellpadding="3" class="right"></td>
</tr>
<tr>
<td cellspacing="3" cellpadding="3" class="left"></td>
<td class="BoldItemText"> </td>
<td class="BoldItemText" align="right">Address</td>
<td class="BoldItemText"> </td>
<td class="ItemText" align="left"><input type=text name="add1" size="40"></td>
<td class="BoldItemText"> </td>
<td cellspacing="3" cellpadding="3" class="right"></td>
</tr>
<tr>
<td cellspacing="3" cellpadding="3" class="left"></td>
<td class="BoldItemText"> </td>
<td class="BoldItemText" align="right"> </td>
<td class="BoldItemText"> </td>
<td class="ItemText" align="left"><input type=text name="add2" size="40"></td>
<td class="BoldItemText"> </td>
<td cellspacing="3" cellpadding="3" class="right"></td>
</tr>
<tr>
<td cellspacing="3" cellpadding="3" class="left"></td>
<td class="BoldItemText"> </td>
<td class="BoldItemText" align="right"> </td>
<td class="BoldItemText"> </td>
<td class="ItemText" align="left"><input type=text name="add3" size="40"></td>
<td class="BoldItemText"> </td>
<td cellspacing="3" cellpadding="3" class="right"></td>
</tr>
<tr>
<td cellspacing="3" cellpadding="3" class="left"></td>
<td class="BoldItemText"> </td>
<td class="BoldItemText" align="right"> </td>
<td class="BoldItemText"> </td>
<td class="ItemText" align="left"><input type=text name="add4" size="40"></td>
<td class="BoldItemText"> </td>
<td cellspacing="3" cellpadding="3" class="right"></td>
</tr>
<tr>
<td cellspacing="3" cellpadding="3" class="left"></td>
<td class="BoldItemText"> </td>
<td class="BoldItemText" align="right">Postcode</td>
<td class="BoldItemText"> </td>
<td class="ItemText" align="left"><input type=text name="postcode" size="15"></td>
<td class="BoldItemText"> </td>
<td cellspacing="3" cellpadding="3" class="right"></td>
</tr>
<tr>
<td cellspacing="3" cellpadding="3" class="left"></td>
<td class="BoldItemText"> </td>
<td class="BoldItemText" align="right">Country</td>
<td class="BoldItemText"> </td>
<td class="ItemText" align="left">
<select name="select3" size="1">
<option value="#"><b>Please Select...</b></option>
<?php
$result3 = mysql_query("SELECT * FROM Country ORDER BY value" )
or die(mysql_error());
while($row3 = mysql_fetch_array( $result3 ))
{
echo "<option value=\"";
echo $row3['idkey'];
echo "\" id=\"";
echo "test";
echo "\"> ";
echo $row3['value'];
echo "</option>\n";
}
echo "</select>\n";
?>
</td>
<td class="BoldItemText"> </td>
<td cellspacing="3" cellpadding="3" class="right"></td>
</tr>
<tr>
<td cellspacing="3" cellpadding="3" class="left"></td>
<td class="BoldItemText"> </td>
<td class="BoldItemText"> </td>
<td class="BoldItemText"> </td>
<td class="BoldItemText" align="left"><input name="submit" type=submit value="Update"></td>
<td class="BoldItemText"> </td>
<td cellspacing="3" cellpadding="3" class="right"></td>
</tr>
<tr>
<td width="40" height="37" class="bottomleft"></td>
<td colspan="5" class="bottom"></td>
<td width="40" height="37" class="bottomright"></td>
</tr>
</table>
<br>
</form>
i am retreiving the fk_country_idkey from the address table ok, but having trouble selecting the correct item for the second dropdown. i have tried all possibilities and not sure how to resolve it.
Thanks in advance.
Copy link to clipboard
Copied
ok, i think i know what the issue is:
if i have these records in the country table:
1 uk
2 france
3 germany
4 italy
10 norway
5 russia
and an email has an fk_country_idkey of 5 (russia) the code picks out the 5th item - Norway and not the record with idkey 5, its almost as if i need to loop through the entire dropdown list for a match and then use selectedindex?
Copy link to clipboard
Copied
resolved. i tried to avoid using javascript, but this was the only way...
<script type="text/javascript">
function getcountry(itemToSelect)
{
var myDropdownList = document.formcheck.select3;
for (iLoop = 0; iLoop< myDropdownList.options.length; iLoop++)
{
if (myDropdownList.options[iLoop].value == itemToSelect)
{
myDropdownList.options[iLoop].selected = true;
break;
}
}
}
</script>
<select name="select2" size="1" onchange="document.getElementById('add1').value = this.options[this.selectedIndex].getAttribute('altvalue1');
document.getElementById('add2').value = this.options[this.selectedIndex].getAttribute('altvalue2');
document.getElementById('add3').value = this.options[this.selectedIndex].getAttribute('altvalue3');
document.getElementById('add4').value = this.options[this.selectedIndex].getAttribute('altvalue4');
document.getElementById('postcode').value = this.options[this.selectedIndex].getAttribute('altvalue5');
getcountry(this.options[this.selectedIndex].getAttribute('altvalue6'));">