petrucci Posted April 18, 2006 Share Posted April 18, 2006 [b]Hi guys. I am really stuck on something i would be grateful any help. i am trying to populate a dropdown box based on another one. i don't know what am i doing wrong.this is the code i'm stuck on(embedded in php code)[/b]<script language=JavaScript><?if (num_rows($res)>0) { $i = num_rows($res); $result = fetch_row($res); $productname = result($result, "productid"); while ($i>0) { // For each product print " var $productname" . "pArray = new Array(\"All\",\n"; while($i>0 and result($result, "productid") == $productname) { // Add Each Build Type $i--; print "\"". result($result, "buildType") ."\",\n"; if ($i>0) { $result = fetch_row($res); } } $productname = result($result, "productid"); print "\"('')\");\n\n"; }}?>function populatebuildType(inForm,selected) { if(selected > '') { var selectedpArray = eval(selected + "pArray"); while (selectedpArray.length < inForm.buildType.options.length) { inForm.buildType.options[(inForm.buildType.options.length - 1)] = null; } for (var i=0; i < selectedpArray.length; i++) { var x = selectedpArray[i]; inForm.buildType.options[i]= new Option( x, x ); } inForm.buildType.options[inForm.buildType.options.length - 1]= null; if (inForm.buildType.options[0].value == '') { inForm.buildType.options[0]= null; if ( navigator.appName == 'Netscape') { if (parseInt(navigator.appVersion) < 4) { window.history.go(0); } else { if (navigator.platform == 'Win32' || navigator.platform == 'Win16') { if(!document.getElementById) window.history.go(0); } } } } }}</script>[b]and i am calling the function here in this form:[/b]<FORM NAME=form1 METHOD=GET ACTION=# STYLE="margin-bottom:1px;"> <TABLE CLASS=CELLBLUE ALIGN=CENTER BORDER=0 CELLPADDING=5 CELLSPACING=0><TR> <TD><B>Date Start:</B><BR><?=calendar_code('datestart', 'Date Start', $datestart)?></TD> <TD><B>Date End:</B><BR><?=calendar_code('dateend', 'Date End', $dateend)?></TD></TR><TR> <TD><B>Product:</B><BR><?=query_select("productid", "productid", "productid", $sqlproduct, $productid, " onchange=populatebuildType(this.form1,this.form1.productid.options[this.form1.productid.selectedIndex].value);")?></TD> <TD><B>Build Type:</B><BR><?=query_select("buildType", "buildType", "buildType", $sqlbuildtype, $buildType)?></TD></TR><TR> <TD ALIGN=CENTER VALIGN=BOTTOM COLSPAN=7><BR><? print submit_button('SUBMIT', 'Filter'); ?></TD></TR></TABLE> </FORM><BR>[b]query_select function creates a select box based on sql query. here is the function[/b]function query_select($selectname, $idfieldname, $descfieldname, $sql, $default="", $other="", $class="AUTHINPUT")// This function generates a HTML select box based on a SQL query// selectname is the name of the select box// idfieldname is the field from the query used as the value of the select box// descfieldname is the field from the query used as the visible description of the select box{ $conn = db_connect(); $res = query($sql, $conn); if (!$res) return(0); $select .= "<SELECT CLASS = $class NAME=\"$selectname\" $other>"; for ($i=0; $i < num_rows($res); $i++) { $result = fetch_row($res); $opt_id = result($result, $idfieldname); $opt_desc = result($result, $descfieldname); $select .= "<OPTION VALUE=\"$opt_id\""; if ($opt_id == $default) { $select .= " SELECTED"; } $select .= ">$opt_desc"; $select .= "</OPTION>"; } $select .= "</SELECT>\n"; return($select);}[b]thanx for any help i appreciate it[/b] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.