Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dropbox and form values with php

Status
Not open for further replies.

hpicken

Technical User
Apr 12, 2001
142
AU
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

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
 
Well, ignoring the PHP (you can figure out how to generate it)
Code:
<html>
<head>
<title>Manufacturers</title>
<script type="text/javascript">
<!--

// Some random details...

var names = new Array();
names["manu1"] = "Manufacturer 1";
names["manu2"] = "Manufacturer 2";
names["manu3"] = "Manufacturer 3";

var ages = new Array();
ages["manu1"] = "136 years";
ages["manu2"] = "28 years";
ages["manu3"] = "3 months";

function fillDetails(f)
{
  var sel = f.elements["manu"];
  var m = sel.options[sel.selectedIndex].value;
  f.elements["name"].value = names[m];
  f.elements["age"].value = ages[m];
}

// -->
</script>
</head>
<body>
<form name="f">
 Select a manufacturer and hit "Show details":<br>
 <select name="manu">
  <option value="manu1">manu1</option>
  <option value="manu2">manu2</option>
  <option value="manu3">manu3</option>
 </select><br>
 <input type="button" value="Show details" onclick="fillDetails(this.form);"> <br>
 Name: <input type="text" name="name"><br>
 Age: <input type="text" name="age"><br>
</form>
</body>
</html>
That what you're looking for?

--Chessbot

"In that blessed region of Four Dimensions, shall we linger on the threshold of the Fifth, and not enter therein? Ah, no! [...] Then, yielding to our intellectual onset, the gates of the Sixth Dimension shall fly open; after that a Seventh, and then an Eighth -- --" Flatland, A. Square (E. A. Abbott)
 
I'll look throught it later and get back to ya.

Cheers

Howard
 
Chessbot, your code would be OK for a single dropdown but I'm using 2. One first to select the Manufacturer and then a second to select a model from that manufacturer.

The first fills the second and the second needs to fill the rest.

So in the case you have given me, you'd select a manufacturer first, then an employee of that manufacturer then their age etc.

Hope that makes sense.

Howard
 
As a followup

the output of second combo list looks like this.

Code:
if (elem.options[elem.selectedIndex].value==1){
document.f1.model.options[document.f1.model.options.length] = new Option('HP1100','85','2500','5');
document.f1.model.options[document.f1.model.options.length] = new Option('HP2000','244','14000','5');
document.f1.model.options[document.f1.model.options.length] = new Option('HP2100','135','5000','5');
document.f1.model.options[document.f1.model.options.length] = new Option('HP2200','140','5000','5');
document.f1.model.options[document.f1.model.options.length] = new Option('HP3100','85','2500','5');
document.f1.model.options[document.f1.model.options.length] = new Option('HP3SI','185','8000','5');
document.f1.model.options[document.f1.model.options.length] = new Option('HP4+','135','6000','5');
document.f1.model.options[document.f1.model.options.length] = new Option('HP4000','170','10000','5');
}
if (elem.options[elem.selectedIndex].value==2){
etc...

So I guess I need some sort of function to parse out the figures after the model.

 
Oh, so just change whatever info and display method you have to hold model info; keep the manufacturer drop down as is.

Tell me if you need an example.

--Chessbot

"In that blessed region of Four Dimensions, shall we linger on the threshold of the Fifth, and not enter therein? Ah, no! [...] Then, yielding to our intellectual onset, the gates of the Sixth Dimension shall fly open; after that a Seventh, and then an Eighth -- --" Flatland, A. Square (E. A. Abbott)
 
I've played with it and can't seem to get my head around it (I hate getting older, it's getting harder to learn new things).

Anyway, an example would be great please...

TIA
Howard
 
Ok... can you give me the layout of the database tables?

--Chessbot

"In that blessed region of Four Dimensions, shall we linger on the threshold of the Fifth, and not enter therein? Ah, no! [...] Then, yielding to our intellectual onset, the gates of the Sixth Dimension shall fly open; after that a Seventh, and then an Eighth -- --" Flatland, A. Square (E. A. Abbott)
 
Here's the whole scheama for you.

Basically the user selects the make and then the model of the unit to calc on. They only have enter the number of prints per month and the rest gets done by the script.

cost = ((toner_cost/prints_per_toner) + drum_cost + fuser_cost + transfer_dev_cost + etc_cost) * copies_month.

Hope that give you the idea.

Code:
-- phpMyAdmin SQL Dump
-- version 2.6.0-pl2
-- [URL unfurl="true"]http://www.phpmyadmin.net[/URL]
-- 
-- Host: localhost
-- Generation Time: Jan 21, 2005 at 02:08 PM
-- Server version: 4.0.23
-- PHP Version: 4.3.10
-- 
-- Database: `auditcalc`
-- 

-- --------------------------------------------------------

-- 
-- Table structure for table `auditlines`
-- 

CREATE TABLE `auditlines` (
  `id` int(9) NOT NULL auto_increment,
  `auditid` int(5) NOT NULL default '0',
  `manufacturer` varchar(150) NOT NULL default '',
  `model` varchar(50) NOT NULL default '',
  `toner_cost` float NOT NULL default '0',
  `pages_per_toner` int(11) NOT NULL default '0',
  `coverage` tinyint(6) NOT NULL default '0',
  `fuser_cost` float NOT NULL default '0',
  `transfer_dev_cost` float NOT NULL default '0',
  `drum_cost` float NOT NULL default '0',
  `etc_cost` float NOT NULL default '0',
  `copies_mth` int(12) NOT NULL default '0',
  `total_cost` float NOT NULL default '0',
  PRIMARY KEY  (`id`),
  KEY `auditid` (`auditid`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;

-- 
-- Dumping data for table `auditlines`
-- 


-- --------------------------------------------------------

-- 
-- Table structure for table `audits`
-- 

CREATE TABLE `audits` (
  `id` int(6) NOT NULL auto_increment,
  `userid` int(4) NOT NULL default '0',
  `client` varchar(150) NOT NULL default '',
  `datecreated` date NOT NULL default '0000-00-00',
  `datemodified` date NOT NULL default '0000-00-00',
  PRIMARY KEY  (`id`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;

-- 
-- Dumping data for table `audits`
-- 


-- --------------------------------------------------------

-- 
-- Table structure for table `manufacturers`
-- 

CREATE TABLE `manufacturers` (
  `man_id` int(3) NOT NULL auto_increment,
  `manufacturer` varchar(150) default NULL,
  `web_site` varchar(250) default NULL,
  PRIMARY KEY  (`man_id`),
  KEY `manufacturer` (`manufacturer`)
) TYPE=MyISAM AUTO_INCREMENT=10 ;

-- 
-- Dumping data for table `manufacturers`
-- 

INSERT INTO `manufacturers` VALUES (1, 'Hewlett Packard', '[URL unfurl="true"]www.hp.com.au');[/URL]
INSERT INTO `manufacturers` VALUES (2, 'Canon', '[URL unfurl="true"]www.canon.com.au');[/URL]
INSERT INTO `manufacturers` VALUES (3, 'Kyocera', '[URL unfurl="true"]www.kyocera.com.au');[/URL]
INSERT INTO `manufacturers` VALUES (4, 'Lexmark', '[URL unfurl="true"]www.lexmark.com.au');[/URL]
INSERT INTO `manufacturers` VALUES (5, 'QMS', NULL);
INSERT INTO `manufacturers` VALUES (6, 'Ricoh', '[URL unfurl="true"]www.ricoh.com.au');[/URL]
INSERT INTO `manufacturers` VALUES (7, 'Tek Pha', NULL);
INSERT INTO `manufacturers` VALUES (8, 'Xerox', NULL);
INSERT INTO `manufacturers` VALUES (9, 'Brother', '[URL unfurl="true"]www.brother.com.au');[/URL]

-- --------------------------------------------------------

-- 
-- Table structure for table `units`
-- 

CREATE TABLE `units` (
  `unit_id` int(11) NOT NULL auto_increment,
  `manufacturer_id` int(11) default '0',
  `model` char(50) default NULL,
  `toner_cost` float default '0',
  `prints_per_toner` int(11) default '0',
  `coverage` smallint(6) default '0',
  `fuser_cost` float default '0',
  `transfer_dev` float default '0',
  `drum_cost` float default '0',
  `etc_cost` float default '0',
  `web_link` char(255) default NULL,
  PRIMARY KEY  (`unit_id`)
) TYPE=MyISAM AUTO_INCREMENT=109 ;

-- 
-- Dumping data for table `units`
-- 

INSERT INTO `units` VALUES (1, 2, 'iR1600', 0, 0, 5, 0, 0, 0, 0, NULL);
INSERT INTO `units` VALUES (2, 2, 'iR2200', 0, 0, 5, 0, 0, 0, 0, NULL);
INSERT INTO `units` VALUES (3, 2, 'iR3300', 0, 0, 5, 0, 0, 0, 0, NULL);
INSERT INTO `units` VALUES (4, 2, 'iR5000', 0, 0, 5, 0, 0, 0, 0, NULL);
INSERT INTO `units` VALUES (5, 2, 'iRC3200b', 0, 25000, 5, 0, 0, 0, 0.03, NULL);
INSERT INTO `units` VALUES (6, 2, 'iRC3200c', 1310, 25000, 5, 0, 0, 0, 0.015, NULL);
INSERT INTO `units` VALUES (7, 2, 'iRC6800b', 0, 40000, 5, 0, 0, 0, 0.012, NULL);
INSERT INTO `units` VALUES (8, 2, 'iRC6800c', 1090, 9500, 5, 0, 0, 0, 0.08, NULL);
INSERT INTO `units` VALUES (9, 2, 'CLC1100b', 0, 10000, 5, 0, 0, 0, 0.05, NULL);
INSERT INTO `units` VALUES (10, 2, 'CLC1100c', 0, 10000, 5, 0, 0, 0, 0.2, NULL);
INSERT INTO `units` VALUES (11, 1, 'HP1100', 85, 2500, 5, 0, 0, 0, 0, NULL);
INSERT INTO `units` VALUES (12, 1, 'HP2000', 244, 14000, 5, 0.006, 0, 0, 0.001, NULL);
INSERT INTO `units` VALUES (13, 1, 'HP2100', 135, 5000, 5, 0, 0, 0, 0, NULL);
INSERT INTO `units` VALUES (14, 1, 'HP2200', 140, 5000, 5, 0.007, 0, 0, 0.003, NULL);
INSERT INTO `units` VALUES (15, 1, 'HP3100', 85, 2500, 5, 0, 0, 0, 0, NULL);
INSERT INTO `units` VALUES (16, 1, 'HP3SI', 185, 8000, 5, 0, 0, 0, 0, NULL);
INSERT INTO `units` VALUES (17, 1, 'HP4+', 135, 6000, 5, 0, 0, 0, 0, NULL);
INSERT INTO `units` VALUES (18, 1, 'HP4000', 170, 10000, 5, 0, 0, 0, 0, NULL);
INSERT INTO `units` VALUES (19, 1, 'HP4050', 170, 10000, 5, 0.005, 0, 0, 0.002, NULL);
INSERT INTO `units` VALUES (20, 1, 'HP4100', 170, 10000, 5, 0.005, 0, 0, 0.002, NULL);
INSERT INTO `units` VALUES (21, 1, 'HP4500b', 180, 9000, 5, 0.0055, 0.0047, 0.02, 0.001, NULL);
INSERT INTO `units` VALUES (22, 1, 'HP4500c', 810, 6000, 5, 0.011, 0.0093, 0.077, 0.001, NULL);
INSERT INTO `units` VALUES (23, 1, 'HP4600B', 300.45, 9000, 5, 0.003, 0.003, 0, 0.001, NULL);
INSERT INTO `units` VALUES (24, 1, 'HP4600C', 1519.95, 8000, 5, 0.0059, 0.0059, 0, 0.001, NULL);
INSERT INTO `units` VALUES (25, 1, 'HP4L', 115, 3000, 5, 0, 0, 0, 0.001, NULL);
INSERT INTO `units` VALUES (26, 1, 'HP4M+', 135, 6000, 5, 0, 0, 0, 0.001, NULL);
INSERT INTO `units` VALUES (27, 1, 'HP4ML', 115, 3000, 5, 0, 0, 0, 0.001, NULL);
INSERT INTO `units` VALUES (28, 1, 'HP4MP', 115, 3000, 5, 0, 0, 0, 0.001, NULL);
INSERT INTO `units` VALUES (29, 1, 'HP4MV', 222, 8000, 5, 0, 0, 0, 0.001, NULL);
INSERT INTO `units` VALUES (30, 1, 'HP4P', 115, 3000, 5, 0, 0, 0, 0.001, NULL);
INSERT INTO `units` VALUES (31, 1, 'HP4P+', 115, 3000, 5, 0, 0, 0, 0.001, NULL);
INSERT INTO `units` VALUES (32, 1, 'HP4SI', 185, 8000, 5, 0, 0, 0, 0.001, NULL);
INSERT INTO `units` VALUES (33, 1, 'HP4V', 222, 8000, 5, 0, 0, 0, 0.001, NULL);
INSERT INTO `units` VALUES (34, 1, 'HP5', 135, 6000, 5, 0, 0, 0, 0.001, NULL);
INSERT INTO `units` VALUES (35, 1, 'HP5000', 225, 10000, 5, 0.006, 0, 0, 0.003, NULL);
INSERT INTO `units` VALUES (36, 1, 'HP5L', 85, 2500, 5, 0, 0, 0, 0.001, NULL);
INSERT INTO `units` VALUES (37, 1, 'HP5M', 140, 6000, 5, 0.006, 0, 0, 0.002, NULL);
INSERT INTO `units` VALUES (38, 1, 'HP5MP', 110, 4000, 5, 0, 0, 0, 0.001, NULL);
INSERT INTO `units` VALUES (39, 1, 'HP5N', 135, 6000, 5, 0, 0, 0, 0.001, NULL);
INSERT INTO `units` VALUES (40, 1, 'HP5P', 110, 4000, 5, 0, 0, 0, 0.001, NULL);
INSERT INTO `units` VALUES (41, 1, 'HP5SI', 260, 15000, 5, 0, 0, 0, 0.001, NULL);
INSERT INTO `units` VALUES (42, 1, 'HP6L', 110, 4000, 5, 0, 0, 0, 0.001, NULL);
INSERT INTO `units` VALUES (43, 1, 'HP6L', 85, 2500, 5, 0, 0, 0, 0.001, NULL);
INSERT INTO `units` VALUES (44, 1, 'HP6MP', 110, 4000, 5, 0, 0, 0, 0.001, NULL);
INSERT INTO `units` VALUES (45, 1, 'HP6P', 110, 4000, 5, 0, 0, 0, 0.001, NULL);
INSERT INTO `units` VALUES (46, 1, 'HP8000', 260, 15000, 5, 0.0039, 0, 0, 0.001, NULL);
INSERT INTO `units` VALUES (47, 1, 'HP8100', 300, 20000, 5, 0, 0, 0, 0.001, NULL);
INSERT INTO `units` VALUES (48, 1, 'HP8500B', 222.85, 15000, 5, 0.0071, 0.0059, 0.0066, 0.001, NULL);
INSERT INTO `units` VALUES (49, 1, 'HP8500C', 1151.25, 7500, 5, 0.0071, 0.0117, 0.0264, 0.001, NULL);
INSERT INTO `units` VALUES (50, 3, 'FS1000', 120, 6000, 5, 0.0029, 0.0026, 0.003, 0.001, NULL);
INSERT INTO `units` VALUES (51, 3, 'FS1010', 120, 6000, 5, 0.0029, 0.0026, 0.003, 0.001, NULL);
INSERT INTO `units` VALUES (52, 3, 'FS1200', 109, 5000, 5, 0.0029, 0.0026, 0.003, 0.001, NULL);
INSERT INTO `units` VALUES (53, 3, 'FS1500', 91, 7000, 5, 0.0029, 0, 0, 0.001, NULL);
INSERT INTO `units` VALUES (54, 3, 'FS1500', 129, 1000, 5, 0.0029, 0, 0, 0.001, NULL);
INSERT INTO `units` VALUES (55, 3, 'FS1600', 129, 1000, 5, 0.0029, 0, 0, 0.001, NULL);
INSERT INTO `units` VALUES (56, 3, 'FS1700', 153, 2000, 5, 0.0029, 0, 0, 0.001, NULL);
INSERT INTO `units` VALUES (57, 3, 'FS1714', 399, 20000, 5, 0.0029, 0, 0, 0.001, NULL);
INSERT INTO `units` VALUES (58, 3, 'FS1750', 153, 2000, 5, 0.0029, 0, 0, 0.001, NULL);
INSERT INTO `units` VALUES (59, 3, 'FS1800', 153, 20000, 5, 0.0029, 0.035, 0.0029, 0.001, NULL);
INSERT INTO `units` VALUES (60, 3, 'FS2200', 80, 3000, 5, 0.0029, 0.0026, 0, 0.001, NULL);
INSERT INTO `units` VALUES (61, 3, 'FS3000', 80, 3000, 5, 0.0029, 0.0038, 0.003, 0.001, NULL);
INSERT INTO `units` VALUES (62, 3, 'FS3010', 80, 3000, 5, 0.0029, 0.0038, 0.003, 0.001, NULL);
INSERT INTO `units` VALUES (63, 3, 'FS3400', 129, 1000, 5, 0.002, 0, 0, 0.001, NULL);
INSERT INTO `units` VALUES (64, 3, 'FS3500', 91, 7000, 5, 0.002, 0, 0, 0.001, NULL);
INSERT INTO `units` VALUES (65, 3, 'FS3600', 129, 1000, 5, 0.002, 0, 0, 0.001, NULL);
INSERT INTO `units` VALUES (66, 3, 'FS3700', 153, 2000, 5, 0.002, 0, 0, 0.001, NULL);
INSERT INTO `units` VALUES (67, 3, 'FS3718', 399, 20000, 5, 0.002, 0, 0, 0.001, NULL);
INSERT INTO `units` VALUES (68, 3, 'FS3750', 153, 2000, 5, 0.002, 0, 0, 0.001, NULL);
INSERT INTO `units` VALUES (69, 3, 'FS3800', 153, 20000, 5, 0.002, 0, 0, 0.001, NULL);
INSERT INTO `units` VALUES (70, 3, 'FS400', 37, 1500, 5, 0.002, 0, 0, 0.001, NULL);
INSERT INTO `units` VALUES (71, 3, 'FS5800CBK', 190, 12000, 5, 0.003, 0, 0, 0.001, NULL);
INSERT INTO `units` VALUES (72, 3, 'FS5800CC', 1296, 10000, 5, 0.003, 0, 0, 0.001, NULL);
INSERT INTO `units` VALUES (73, 3, 'FS5900CBK', 165, 12000, 5, 0.003, 0, 0, 0.001, NULL);
INSERT INTO `units` VALUES (74, 3, 'FS5900CC', 1122, 10000, 5, 0.003, 0, 0, 0.001, NULL);
INSERT INTO `units` VALUES (75, 3, 'FS600', 84, 3600, 5, 0, 0, 0, 0.001, NULL);
INSERT INTO `units` VALUES (76, 3, 'FS6500', 129, 1000, 5, 0, 0, 0, 0.001, NULL);
INSERT INTO `units` VALUES (77, 3, 'FS6700', 153, 2000, 5, 0, 0, 0, 0.001, NULL);
INSERT INTO `units` VALUES (78, 3, 'FS680', 84, 3600, 5, 0, 0, 0, 0.001, NULL);
INSERT INTO `units` VALUES (79, 3, 'FS6900', 153, 20000, 5, 0, 0, 0, 0.001, NULL);
INSERT INTO `units` VALUES (80, 3, 'FS6900', 153, 2000, 5, 0, 0, 0, 0.001, NULL);
INSERT INTO `units` VALUES (81, 3, 'FS7000', 219, 3300, 5, 0, 0, 0, 0.001, NULL);
INSERT INTO `units` VALUES (82, 3, 'FS800', 84, 3600, 5, 0.0029, 0.0054, 0.0029, 0.001, NULL);
INSERT INTO `units` VALUES (83, 3, 'FS8000CBK', 179, 25000, 5, 0, 0, 0, 0.001, NULL);
INSERT INTO `units` VALUES (84, 3, 'FS8000CC', 587, 1000, 5, 0, 0, 0, 0.001, NULL);
INSERT INTO `units` VALUES (85, 3, 'FS9000', 219, 3300, 5, 0, 0, 0, 0.001, NULL);
INSERT INTO `units` VALUES (86, 3, 'LS1550', 129, 1000, 5, 0, 0, 0, 0.001, NULL);
INSERT INTO `units` VALUES (87, 3, 'LS3550', 129, 1000, 5, 0, 0, 0, 0.001, NULL);
INSERT INTO `units` VALUES (88, 3, 'LS6500', 129, 1000, 5, 0, 0, 0, 0.001, NULL);
INSERT INTO `units` VALUES (89, 3, 'LS6550', 129, 1000, 5, 0, 0, 0, 0.001, NULL);
INSERT INTO `units` VALUES (90, 4, 'OPTRAC1200', 810, 6500, 5, 0.006, 0, 0.043, 0.02, NULL);
INSERT INTO `units` VALUES (91, 4, 'OPTRAM410', 180, 5000, 5, 0, 0.00025, 0, 0.001, NULL);
INSERT INTO `units` VALUES (92, 4, 'OPTRAN', 260, 15000, 5, 0, 0, 0, 0.001, NULL);
INSERT INTO `units` VALUES (93, 4, 'OPTRAS', 250, 7500, 5, 0, 0, 0, 0.001, NULL);
INSERT INTO `units` VALUES (94, 4, 'OPTRASC1275', 0, 0, 0, 0, 0, 0, 0, NULL);
INSERT INTO `units` VALUES (95, 4, 'OPTRAT', 250, 10000, 5, 0.002, 0, 0, 0.0015, NULL);
INSERT INTO `units` VALUES (96, 4, 'OPTRAW810', 210, 20000, 5, 0.0025, 0, 0.0078, 0.001, NULL);
INSERT INTO `units` VALUES (97, 5, 'QMS1725', 251, 8000, 5, 0.002, 0, 0, 0.001, NULL);
INSERT INTO `units` VALUES (98, 5, 'QMS860', 290, 7500, 5, 0, 0, 0, 0.001, NULL);
INSERT INTO `units` VALUES (99, 6, 'AFI340', 30, 15000, 5, 0, 0, 0, 0.012, NULL);
INSERT INTO `units` VALUES (100, 6, 'FT3313', 30, 8000, 5, 0, 0, 0, 0, NULL);
INSERT INTO `units` VALUES (101, 6, 'FT3522', 30, 8000, 5, 0, 0, 0, 0, NULL);
INSERT INTO `units` VALUES (102, 6, 'FT4822', 30, 8000, 5, 0, 0, 0, 0.01, NULL);
INSERT INTO `units` VALUES (103, 7, 'Tekpha850b', 214.5, 12000, 5, 0, 0, 0, 0, NULL);
INSERT INTO `units` VALUES (104, 7, 'Tekpha850c', 270, 1180, 5, 0, 0, 0, 0, NULL);
INSERT INTO `units` VALUES (105, 8, 'ABL1321', 0, 0, 0, 0, 0, 0, 0, NULL);
INSERT INTO `units` VALUES (106, 8, 'Viv450', 0, 0, 0, 0, 0, 0, 0, NULL);
INSERT INTO `units` VALUES (107, 8, 'Viv500', 0, 0, 0, 0, 0, 0, 0, NULL);
INSERT INTO `units` VALUES (108, 9, 'HL-1660', 0, 0, 0, 0, 0, 0, 0, NULL);

Some of the values will be zero
 
Something like this?
Code:
<?php
// log into database
echo <<<END
<html>
<head>
<script type="text/javascript">
<!--

var units = new Array();
END;

$sql = "SELECT unit_id, manufacturer_id, model, toner_cost, prints_per_toner, coverage, fuser_cost, transfer_dev, drum_cost, etc_cost, web_link FROM units";
$units = mysql_result($sql);
$flds = array();

while ($row = mysql_fetch_assoc($units))
{
  $id = $row['unit_id'];
  echo 'units[' . $id . '] = new Array();\n';
  foreach ($row as $key=>$val)
  {
    echo 'units[' . $id . ']["' . $key . '"] = "' . $val . '"';
    if ($id == 0)
      $flds[] = $key;
  }
}

echo <<<END

function fillSel(f)
{
  var src = f.elements["manufacturers"];
  var targ = f.elements["units"];
  var manid = parseInt(src.options[src.selectedIndex].value);
  targ.options.length = 0;
  for (var i=0; i<units[i].length; i++)
  {
    if (units[i]["manufacturer_id"] == manid)
      targ.options[targ.options.length] = new Option(units[i]["model"], units[i]["model"]);
  }
}

function fillInfo(f)
{
  var src = f.elements["units"];
  var id = parseInt(src.options[src.selectedIndex].value);
  for (n in units[id])
    f.elements[n].value = units[id][n];
}

// -->
</script>
</head>
<body>
<form name="f" action="submit.php" method="post">
Manufacturers:
<select name="manufacturers" onchange="fillSel(this.form);"> 
END;
$sql = "SELECT man_id, manufacturer FROM manufacturers";
$manu = mysql_result($sql);

while ($row = mysql_fetch_assoc($manu))
{
  echo '<option value="' . $row["man_id"] . '">' . $row["manufacturer"] . '</option>\n';
}

echo <<<END
</select><br>
Units:
<select name="units" onchange="fillInfo(f);">
END;

$sql = "SELECT unit_id, model FROM units";
$u = mysql_result($sql);

while ($row = mysql_fetch_assoc($u))
{
  echo '<option value="' . $row['unit_id'] . '">' . $row['model'] . '</option>\n';
}

echo <<<END
</select><br>
Info:<br>
END;

foreach ($flds as $fld)
{
  echo $fld . ': <input type="text" name="' . $fld . '"><br>\n';
}

echo <<<END
</form>
</body>
</html>
END;

?>

That give you enough to go on?

--Chessbot

"In that blessed region of Four Dimensions, shall we linger on the threshold of the Fifth, and not enter therein? Ah, no! [...] Then, yielding to our intellectual onset, the gates of the Sixth Dimension shall fly open; after that a Seventh, and then an Eighth -- --" Flatland, A. Square (E. A. Abbott)
 
Looks good Chessbot.

I'll have a look at it over the weekend.

Thanks and cheers for now

Howard
 
Okay...

I had and look and a try of your script Chessbot.

I had to make a change, mysql_result to mysql_query, but it doesn't seem to be narrowing the units to the man so any man can have any model.

If I change line

echo 'units[' . $id . ']["' . $key . '"]="' . $val . '"';

to

echo 'units[' . $id . ']["' . $key . '"]="' . $val . '";';

so that the elements are seperated it then doesn't fill the model dropdown when a man is selected.

Many thanks for your assistance.

Howard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top