I need a way to have 6 text field populate dynamically from the user selection of a drop down menu value. I have seen a number of threads about this, but they are not working for me.
I have a drop down menu that is dynamically filled with the company names from a mySQL database. (this works)
What I need is for the text fields (description, address, city, state, zip, phone) to fill with the information linked to the drop down selection.
This is what I have so far...
Any help would be greatly appreciated.
Thanks,
Lester
I have a drop down menu that is dynamically filled with the company names from a mySQL database. (this works)
What I need is for the text fields (description, address, city, state, zip, phone) to fill with the information linked to the drop down selection.
This is what I have so far...
Code:
<?php
...
mysql_select_db($database_connection, $connection);
$query_franchises = "SELECT company, description, address, city, state, zip, phone FROM Franchise ORDER BY id ASC";
$franchises = mysql_query($query_franchises, $connection) or die(mysql_error());
$row_franchises = mysql_fetch_assoc($franchises);
$totalRows_franchises = mysql_num_rows($franchises);
?>
...
<script type="text/javascript">
function selectCompany(selObj) {
var companyArray[<?php $row_franchises['description']; ?>,
<?php $row_franchises['address']; ?>,
<?php $row_franchises['city']; ?>,
<?php $row_franchises['state']; ?>,
<?php $row_franchises['zip']; ?>,
<?php $row_franchises['phone']; ?>];
var id = selObj.options[selObj.selectedIndex].value;
document.getElementById('description').value = companyArray[id][0];
document.getElementById('address').value = companyArray[id][1];
document.getElementById('city').value = companyArray[id][2];
document.getElementById('state').value = companyArray[id][3];
document.getElementById('zip').value = companyArray[id][4];
document.getElementById('telephone').value = companyArray[id][5];
}
</script>
...
<select name="franchise" id="franchise" onchange="selectCompany(this)">
<?php do
{ ?>
<option value="<?php echo $row_franchises['company']?>"
<?php if (!(strcmp($row_franchises['company'], $row_franchises['company'])))
{echo "selected=\"selected\"";} ?>><?php echo $row_franchises['company']?></option>
<?php }
while ($row_franchises = mysql_fetch_assoc($franchises));
$rows = mysql_num_rows($franchises);
if($rows > 0) {
mysql_data_seek($franchises, 0);
$row_franchises = mysql_fetch_assoc($franchises);
}
?>
</select>
...
<tr>
<td><input name="description" type="text" id="description" value=""></td>
</tr>
<tr>
<td><input name="address" type="text" id="address" value=""></td>
</tr>
<tr>
<td><input name="city" type="text" id="city" value=""></td>
</tr>
<tr>
<td><input name="state" type="text" id="state" value=""></td>
</tr>
<tr>
<td><input name="zip" type="text" id="zip" value=""></td>
</tr>
<tr>
<td><input name="telephone" type="text" id="telephone" value=""></td>
</tr>
...
Any help would be greatly appreciated.
Thanks,
Lester