Help returning values from array
Posted: Sat Jan 29, 2005 12:54 am
Hi folks
I'm playing with a form to do some calcs using PHP/MySQL & Javascript.
Basically, I have two select fields where a user can select a manufacturer then a model for that man. The user then enters the number of prints they do a month and the script will calc what that unit is costing them each month.
So far I have the script grabbing the data and filling out the select boxes correctly. What I'm having problems with is getting the rest of the data to the form after the model has been selected.
A view source of the script so far is
The problem is the getdetails function and not knowing javascript, I finding it a bit of a problem to work out.
Any help would be appreciated.
TasTech
I'm playing with a form to do some calcs using PHP/MySQL & Javascript.
Basically, I have two select fields where a user can select a manufacturer then a model for that man. The user then enters the number of prints they do a month and the script will calc what that unit is costing them each month.
So far I have the script grabbing the data and filling out the select boxes correctly. What I'm having problems with is getting the rest of the data to the form after the model has been selected.
A view source of the script so far is
Code: Select all
<form name="form1" action='' method="post">
Manufacturer:
<select name="manufacturer" size="1" onchange="manufacturerselected(this);">
<option value="#" selected>Please Select</option>
<option value='1'>Hewlett Packard</option>
<option value='2'>Canon</option>
<option value='3'>Kyocera</option>
<option value='4'>Lexmark</option>
<option value='5'>QMS</option>
<option value='6'>Ricoh</option>
<option value='7'>Tek Pha</option>
<option value='8'>Xerox</option>
<option value='9'>Brother</option>
</select>
<br />
Model:
<select name="model" size="1" onchange="getdetails();">
<option>їno model selected]</option>
</select>
<br />
Toner cost: <input type="text" name="toner_cost" size="10">
Print Outs: <input type="text" name="prints_per_toner" size="10">
Toner Coverage: <input type="text" name="coverage" size="10">
Fuser Cost: <input type="text" name="fuser_cost" size="10">
Transfer Cost: <input type="text" name="transfer_dev" size="10">
Drum Cost: <input type="text" name="drum_cost" size="10">
Other Costs: <input type="text" name="etc_cost" size="10">
Months Prints: <input type="text" name="monthusage" size="10">
Total Cost: <input type "text" name="costtotal" size="10">
<input type="submit" name="submitunit" value="submit"><br />
<script language="JavaScript">
function manufacturerselected(elem){
for (var i = document.form1.model.options.length; i >= 0; i--) {
document.form1.model.optionsїi] = null;
}
if (elem.optionsїelem.selectedIndex].value==1){
document.form1.model.optionsїdocument.form1.model.options.length] = new Option('iR1600','1');
document.form1.model.optionsїdocument.form1.model.options.length] = new Option('iR2200','2');
document.form1.model.optionsїdocument.form1.model.options.length] = new Option('iR3300','3');
document.form1.model.optionsїdocument.form1.model.options.length] = new Option('iR5000','4');
document.form1.model.optionsїdocument.form1.model.options.length] = new Option('iRC3200b','5');
document.form1.model.optionsїdocument.form1.model.options.length] = new Option('iRC3200c','6');
document.form1.model.optionsїdocument.form1.model.options.length] = new Option('iRC6800b','7');
document.form1.model.optionsїdocument.form1.model.options.length] = new Option('iRC6800c','8');
document.form1.model.optionsїdocument.form1.model.options.length] = new Option('CLC1100b','9');
document.form1.model.optionsїdocument.form1.model.options.length] = new Option('CLC1100c','10');
}
if (elem.optionsїelem.selectedIndex].value==2){
document.form1.model.optionsїdocument.form1.model.options.length] = new Option('HP1100','11');
document.form1.model.optionsїdocument.form1.model.options.length] = new Option('HP2000','12');
document.form1.model.optionsїdocument.form1.model.options.length] = new Option('HP2100','13');
document.form1.model.optionsїdocument.form1.model.options.length] = new Option('HP2200','14');
}
if (elem.optionsїelem.selectedIndex].value==3){
document.form1.model.optionsїdocument.form1.model.options.length] = new Option('FS1000','50');
document.form1.model.optionsїdocument.form1.model.options.length] = new Option('FS1010','51');
}
// <----- etc ------>
}
// <--- All working up this point ------>
function getdetails() {
var src = document.form1.model.value;
var id = parseInt(src);
for (n in unitsїid]) {
document.form1.elementsїn].value = unitsїid]їn];
}
}
var units = new Array();
unitsї1] = new Array();
unitsї1]ї'manufacturer_id']='2';
unitsї1]ї'unit_id']='1';
unitsї1]ї'toner_cost']='0';
unitsї1]ї'prints_per_toner']='50000';
unitsї1]ї'coverage']='5';
unitsї1]ї'fuser_cost']='0.13';
unitsї1]ї'transfer_dev']='0.034';
unitsї1]ї'drum_cost']='0.021';
unitsї1]ї'etc_cost']='0';
//<------- etc ------->
unitsї108] = new Array();
unitsї108]ї'manufacturer_id']='9';
unitsї108]ї'unit_id']='108';
unitsї108]ї'toner_cost']='100';
unitsї108]ї'prints_per_toner']='8000';
unitsї108]ї'coverage']='5';
unitsї108]ї'fuser_cost']='0';
unitsї108]ї'transfer_dev']='0';
unitsї108]ї'drum_cost']='1.5';
unitsї108]ї'etc_cost']='1.3';
</script>Any help would be appreciated.
TasTech