Page 1 of 1

Help returning values from array

Posted: Sat Jan 29, 2005 12:54 am
by tastech
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

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>&#1111;no model selected]</option>
</select>
<br />

Toner cost:&nbsp;<input type="text" name="toner_cost" size="10">
Print Outs:&nbsp;<input type="text" name="prints_per_toner" size="10">
Toner Coverage:&nbsp;<input type="text" name="coverage" size="10">
Fuser Cost:&nbsp;<input type="text" name="fuser_cost" size="10">
Transfer Cost:&nbsp;<input type="text" name="transfer_dev" size="10">
Drum Cost:&nbsp;<input type="text" name="drum_cost" size="10">
Other Costs:&nbsp;<input type="text" name="etc_cost" size="10">
Months Prints:&nbsp;<input type="text" name="monthusage" size="10">
Total Cost:&nbsp;<input type "text" name="costtotal" size="10">
<input type="submit" name="submitunit" value="submit"><br />

<script language="JavaScript">
function manufacturerselected(elem)&#123;
     for (var i = document.form1.model.options.length; i >= 0; i--) &#123;
	document.form1.model.options&#1111;i] = null;
     &#125;
	if (elem.options&#1111;elem.selectedIndex].value==1)&#123;
	document.form1.model.options&#1111;document.form1.model.options.length] = new Option('iR1600','1');
	document.form1.model.options&#1111;document.form1.model.options.length] = new Option('iR2200','2');
	document.form1.model.options&#1111;document.form1.model.options.length] = new Option('iR3300','3');
	document.form1.model.options&#1111;document.form1.model.options.length] = new Option('iR5000','4');
	document.form1.model.options&#1111;document.form1.model.options.length] = new Option('iRC3200b','5');
	document.form1.model.options&#1111;document.form1.model.options.length] = new Option('iRC3200c','6');
	document.form1.model.options&#1111;document.form1.model.options.length] = new Option('iRC6800b','7');
	document.form1.model.options&#1111;document.form1.model.options.length] = new Option('iRC6800c','8');
	document.form1.model.options&#1111;document.form1.model.options.length] = new Option('CLC1100b','9');
	document.form1.model.options&#1111;document.form1.model.options.length] = new Option('CLC1100c','10');
	&#125;
     if (elem.options&#1111;elem.selectedIndex].value==2)&#123;
	document.form1.model.options&#1111;document.form1.model.options.length] = new Option('HP1100','11');
	document.form1.model.options&#1111;document.form1.model.options.length] = new Option('HP2000','12');
	document.form1.model.options&#1111;document.form1.model.options.length] = new Option('HP2100','13');
	document.form1.model.options&#1111;document.form1.model.options.length] = new Option('HP2200','14');
	&#125;

	if (elem.options&#1111;elem.selectedIndex].value==3)&#123;
	document.form1.model.options&#1111;document.form1.model.options.length] = new Option('FS1000','50');
	document.form1.model.options&#1111;document.form1.model.options.length] = new Option('FS1010','51');
	&#125;
	// <-----  etc  ------>
&#125;

// <--- All working up this point ------>

function getdetails() &#123;
  var src = document.form1.model.value;
  var id = parseInt(src);
  for (n in units&#1111;id]) &#123;
    document.form1.elements&#1111;n].value = units&#1111;id]&#1111;n];
  &#125;
&#125;

var units = new Array();

units&#1111;1] = new Array();
units&#1111;1]&#1111;'manufacturer_id']='2';
units&#1111;1]&#1111;'unit_id']='1';
units&#1111;1]&#1111;'toner_cost']='0';
units&#1111;1]&#1111;'prints_per_toner']='50000';
units&#1111;1]&#1111;'coverage']='5';
units&#1111;1]&#1111;'fuser_cost']='0.13';
units&#1111;1]&#1111;'transfer_dev']='0.034';
units&#1111;1]&#1111;'drum_cost']='0.021';
units&#1111;1]&#1111;'etc_cost']='0';

//<-------  etc ------->

units&#1111;108] = new Array();
units&#1111;108]&#1111;'manufacturer_id']='9';
units&#1111;108]&#1111;'unit_id']='108';
units&#1111;108]&#1111;'toner_cost']='100';
units&#1111;108]&#1111;'prints_per_toner']='8000';
units&#1111;108]&#1111;'coverage']='5';
units&#1111;108]&#1111;'fuser_cost']='0';
units&#1111;108]&#1111;'transfer_dev']='0';
units&#1111;108]&#1111;'drum_cost']='1.5';
units&#1111;108]&#1111;'etc_cost']='1.3';

</script>
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

Posted: Sat Feb 12, 2005 10:34 pm
by kaYak
This page may be useful to you.