Hi folks, I have a form I want to use for adding info to a backend db (duh!). What I want to do is select a manufacturer from a list then a model for that manufacturer and have the model select box fill in the other details to text fields.
The code I'm playing with (and I apologise to the original unknown author who credit must got to) is below
It all works except for the filling out of the other details. I really know nothing about js but I know I need to use it for this app so if someone could give a hand, it would be much appreciated.
Thanks
Howard
The code I'm playing with (and I apologise to the original unknown author who credit must got to) is below
Code:
<html>
<title>PHP/MySQL - Dynamic DropDowns</title>
<body>
<?php
//Database Variables
$db_Database = "auditcalc";
$db_UserName = "user";
$db_Password = "password";
$db_Hostname = "localhost";
//Connect to the Database
mysql_connect($db_Hostname, $db_UserName, $db_Password) || UhOh("Can't Connect to Database: ".mysql_error());
mysql_select_db($db_Database);
echo "<form name=\"f1\" action='$PHP_SELF' method=\"post\">\n";
//read the database
$result = mysql_query("SELECT manufacturers.manufacturer,units.manufacturer_id,units.model,units.unit_id,units.toner_cost,units.print_mth,units.coverage FROM manufacturers,units WHERE units.manufacturer_id=manufacturers.man_id");
//write the table
echo "<table width=\"200\" border=\"0\" cellpadding=\"5\" cellspacing=\"5\">";
// write the manufacturers's listbox...
echo "\n<tr><td valign=\"middle\" align=\"center\"><font color=\"#000000\" face=\"verdana,arial,helvetica\" size=\"2\">Manufacturer</font></td>\n<td><select name=\"manufacturer\" size=\"1\" onchange=\"manufacturerselected(this);\">\n";
// write the entry code for the javascript...\n is used to force a new line so the resultant code is more readable
$sJavaScript = "function manufacturerselected(elem){\n for (var i = document.f1.model.options.length; i >= 0; i--){ \n document.f1.model.options[i] = null;\n";
// loop through the database..
$sLastManufacturer="";
echo "<option value=\"#\" selected>Please Select</option>\n";
while ( $row = mysql_fetch_array($result)) {
// is this a new manufacturer?
If ($sLastManufacturer!=$row["manufacturer"]){
// if yes, add the entry to the manufacturer listbox
$sLastManufacturer = $row["manufacturer"];
echo "\n<option value='".$row["manufacturer_id"]."'>".$sLastManufacturer."</option>";
// and add a new section to the javascript...
$sJavaScript = $sJavaScript."}\n"."if (elem.options[elem.selectedIndex].value==".$row["manufacturer_id"]."){\n";
}
// and add a new model line to the javascript
$sJavaScript = $sJavaScript."document.f1.model.options[document.f1.model.options.length] = new Option('".$row["model"]."','".$row["unit_id"]."');\n";
}
// finish the country's listbox
echo "</select></td></tr>";
// create the city listbox for no selection
echo "\n<tr><td valign=\"center\" align=\"center\"><font color=\"#000000\" face=\"verdana,arial,helvetica\" size=\"2\">Model</font></td>\n<td><select name=\"model\" size=\"1\">\n";
echo "<option>[no model selected]</option>\n";
echo "</select></td></tr>\n";
echo "<tr><td>Toner cost</td><td><input type=\"text\" name=\"toner_cost\" size=\"10\"></td></tr>";
echo "<tr><td>Print Outs</td><td><input type=\"text\" name=\"print_mth\" size=\"10\"></td></tr>";
echo "<tr><td>Toner Coverage</td><td><input type=\"text\" name=\"coverage\" size=\"10\"></td></tr>";
echo "<tr><td align=\"center\" colspan=\"2\"><input type=\"submit\" name=\"submitunit\" value=\"submit\"></td></tr>\n";
echo "</table>\n";
// finish the javascript and write out
$sJavaScript = $sJavaScript."\n}\n}\n";
echo "\n<script language=\"JavaScript\">";
echo "\n".$sJavaScript."\n";
echo "</script>\n";
//close the form
echo "</form></center>";
if ("submit" == $submitunit){
echo "<font color=\"#000000\" face=\"verdana,arial,helvetica\" size=\"2\">Your Selected Country index= ".$manufacturer."</font><br>";
echo "<font color=\"#000000\" face=\"verdana,arial,helvetica\" size=\"2\">Your Selected City index= ".$model."</font><br>";
}
?>
</body>
</html>
It all works except for the filling out of the other details. I really know nothing about js but I know I need to use it for this app so if someone could give a hand, it would be much appreciated.
Thanks
Howard