Hi,
I have 3 dropdown boxes populated from database. I want to refresh 2 nd dropdown boxes depending on the value selected in first drop down. I am trying with PHP and Javascript code but some how it's not changing the data, actually it's not populating the array from where it is populating the 2 nd drop box.
I think the way I am calling javascript functions in my php functions is wrong. Please help
Thanks in advance,
Shankar
<?
// --------------------------------------------------------
// Get Idc Names (Location) for drop-down list display
// --------------------------------------------------------
function getIdcNames ($idc) {
$query="select idcname from idcnames order by idcname";
$result_id = mysql_query($query)
or die("getIdcNames:" . mysql_error() );
while ( $row=mysql_fetch_array($result_id) ) {
print("<OPTION ");
if ( $idc == $row[0] ) {
print("SELECTED ");
}
print("VALUE=$row[0]>$row[0]\n");
$arrayIdc[] = $row[0];
}
return ($arrayIdc);
} // getIdcNames
// --------------------------------------------------------
// Get Manufacturer for drop-down list display
// --------------------------------------------------------
function getMfg ($mfg) {
echo "mfg-". $mfg;
$query="select distinct mfg from products order by mfg";
$result_id = mysql_query($query)
or die("getMfg:" . mysql_error() );
while ( $row=mysql_fetch_array($result_id) ) {
print("<OPTION ");
if ( $idc == $row[0] ) {
print("SELECTED ");
}
print("VALUE=$row[0]>$row[0]\n");
$arrayMfg[] = $row[0];
}
return ($arrayMfg);
} // getMfg
// --------------------------------------------------------
// Produce statm to make JavaScript calls
// --------------------------------------------------------
function getSubOption3($arrayMfg) {
$idx=count($arrayMfg);
//print("idx=$idx \n");
for($i=0; $i<$idx; $i++) {
$query="select distinct series from products
where mfg = \"$arrayMfg[$i]\"
order by mfg";
// print("query= $query");
echo "<BR>\n";
echo "setupMfg();\n";
echo "setupMfg();\n";
$result_id = mysql_query($query)
or die("display_db_query:" . mysql_error() );
$itemCount=0;
while ( $row=mysql_fetch_array($result_id) ) {
echo "setupItem(\"$row[0]\",$row[0]);\n";
$itemCount++; //track any item for this Mfg
//echo "itemCount-". $itemCount;
} //while
if ( $itemCount == 0 ) {
echo "setupItem(\"subItem\",0);\n";
}
}
} // getSubOption3
?>
<HEAD>
<TITLE>Search</TITLE>
</HEAD>
<SCRIPT LANGUAGE="JavaScript">
var arrayCat = new Array(20);
var catIndex = 0;
var itemIndex;
function setupMfg() {
catIndex++;
alert (catIndex);
arrayCat[catIndex] = new Array(); //1;
itemIndex = 1;
}
function setupItem(text,value) {
arrayCat[catIndex][itemIndex]=new myOptions(text,value); //1,1; 1,2; 1,3
itemIndex++;
}
function myOptions(text,value) {
this.text = text;
this.value = value;
}
function setSeries(j) {
var f = document.main;
var text;
var value;
var idx;
idx = f.series.options.length-1;
for(i=idx; i>0; i--){
f.series.options = null; }
text = 'ALL';
value = 0;
f.series.options[0] = new Option(text, value);
if ( j > 0 ) {
for(i=0; i<arrayCat[j].length-1; i++) {
text = arrayCat[j][i+1].text;
value = arrayCat[j][i+1].value;
f.series.options[i+1] = new Option(text, value);
}
}
} //setSeries
</SCRIPT>
<BODY>
<FORM NAME="main" METHOD=POST ACTION="<?php print("$PHP_SELF"); ?>">
<TABLE BORDER=0>
<TR>
<TD>
<B>Manufacturer:</B>
<SELECT NAME=mfg SIZE=1 onChange="setSeries(this.selectedIndex)">
<OPTION VALUE=0>ALL
<?php
$arrayMfg = getMfg( $mfg );
?>
</SELECT> </P>
</TD>
<?php
getSubOption3($arrayMfg);
?>
<TD>
<B>Series:</B>
<SELECT NAME=series SIZE=1>
<OPTION VALUE=0>ALL
<?php
$arraySeries = getSeries( $mfg, $series );
?>
</SELECT> </P>
</TD>
</TR>
</TABLE>
</FORM>
</BODY>