suttercain Posted February 28, 2007 Share Posted February 28, 2007 Hello, I am trying to get the second drop down menu populated based on the selection of the first. I have dont this before but I don't know what am I doing wrong this time. So the user selects the YEAR within the first drop down. This then reloads the page and populates the second drop from the MySQL database using the column 'division' The Code: <?php //Connect to server require ('get_connected.php'); import_request_variables("gpc",""); function show_year($year) { $year_arr = array("","2006","2007"); $arr_size = count($year_arr); for ($i = 0; $i < $arr_size; $i++) { if ($year_arr[$i] == $year) print "<option selected value=\"" .$year_arr[$i] ."\">".$year_arr[$i] ."</option>\n"; else print "<option value=\"" .$year_arr[$i] ."\">".$year_arr[$i] ."</option>\n"; } } function show_division($year,$div) { //Result SQL statement $sql="SELECT * FROM " .$year. "data GROUP by division"; //Count the number of results $result = mysql_query("SELECT * FROM " .$year. "data GROUP by division"); //$rows = mysql_result($result, 0, 'total'); $rows = mysql_num_rows($result); //Populate the drop down list $addblankspace = ""; print "<option value=\"" .$addblankspace ."\">".$addblankspace ."</option>\n"; for ($i = 0; $i < $rows; $i++) { $tempdivision = mysql_result($sql_result,$i,'division'); if ($tempdivision == $div) print "<option selected value=\"" .$tempdivision."\">".$tempdivison."</option>\n"; else print "<option value=\"" .$tempdivision ."\">".$tempdivision ."</option>\n"; } $sql = ""; //Close the database connection mysql_close(); } ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"> <HTML> <script language=javascript> function showyear(year) { refresh = "./verdevdatabase.php?year=" + year; location.href=refresh; } function showmodel(div,year) { refresh = "./verdevdatabase.php?man=" + div + "&year=" + year ; location.href=refresh; } </script> <BODY> <FORM ACTION="verdevquery.php" METHOD="POST" ENCTYPE="application/x-www-form-urlencoded"> <P ALIGN="CENTER"></P> <P ALIGN="CENTER"><B><FONT SIZE="4">VERDEV DATABASE SEARCH</FONT></B></P> <CENTER> <P><BR> <TABLE BORDER="1" CELLPADDING="0" CELLSPACING="0" WIDTH="379"> <TR BGCOLOR="#0000CC"> <TD WIDTH="375"> <DIV ALIGN="CENTER"> <P><SPAN CLASS="style3"><FONT SIZE="4" COLOR="white">SEARCH BY<BR> YEAR, MANUFACTURE and MODEL</FONT></SPAN> </DIV> </TD> </TR> <TR> <TD WIDTH="375"> <CENTER> <P> <TABLE BORDER="0" CELLPADDING="0" CELLSPACING="5" WIDTH="257" HEIGHT="139"> <TR> <TD WIDTH="133"><SPAN CLASS="style1"><FONT SIZE="4" FACE="Arial, Helvetica">Year:</FONT></SPAN></TD> <TD WIDTH="103"> <P align="left"> <SELECT NAME=year onchange=showyear(this.options[this.selectedIndex].value)> <?php show_year($year); ?> </SELECT> </TD> </TR> <TR> <TD WIDTH="133" class="style1"><FONT SIZE="4" FACE="Arial, Helvetica">Division:</FONT></TD> <TD WIDTH="103"> <P align="left"> <SELECT NAME=divison onchange=showmodel(this.options[this.selectedIndex].value,year.value)> <?php show_division($year,$div); ?> </SELECT> </TD> </TR> <TR> <TD COLSPAN="2"> <CENTER> <P><INPUT TYPE="SUBMIT" NAME="searchtype" VALUE="Search by YMM"> </CENTER> </TD> </TR> </TABLE> </CENTER> </TD> </TR> </TABLE> </CENTER> <P> </FORM> </BODY> </HTML> Can anyone assist? Thank you. Shannon Link to comment https://forums.phpfreaks.com/topic/40566-populate-second-drop-down-based-on-selection-of-first/ Share on other sites More sharing options...
boo_lolly Posted February 28, 2007 Share Posted February 28, 2007 i've done the same thing... here is my code. i basically use a switch statement inside an IF/isset statement: <?php echo "<TABLE BORDER=\"0\"><TR>\n"; echo "<TD align=\"left\" valign=\"bottom\">"; echo "<form action=\"". $_SERVER['php_self'] ."?regID=". $regID ."\" method=\"post\">\n"; echo "<SELECT NAME=\"categories\">\n"; echo "<OPTION VALUE=\"\">Choose a Category\n"; /*query category info*/ $cat_sql = mysql_query("SELECT * FROM categories") or die(mysql_error()); /*populate dropdown menu from the shopping cart product table*/ while($cat_row = mysql_fetch_array($cat_sql)){ echo "<OPTION VALUE=\"". $cat_row['id'] ."\"". (($_POST['categories'] == $cat_row['id']) ? (" SELECTED") : ("")) .">". $cat_row['name']. "\n"; } echo "</SELECT>\n"; echo "<input type=\"submit\" value=\">>\">\n"; echo "</form>\n"; echo "</TD>"; /* *if a category has been chosen, *use this switch statement to populate *the item list that falls under the *chosen category */ if(isset($_POST['categories']) && !empty($_POST['categories'])){ switch($_POST['categories']){ case $_POST['categories']: /*get the items that are in that category*/ $item_sql = mysql_query("SELECT id, name, cat FROM items WHERE cat = ". $_POST['categories'] ."") or die(mysql_error()); echo "<TD align=\"left\" valign=\"bottom\">"; echo "<form action=\"addItem.php?regID=". $_GET['regID'] ."\" method=\"post\">\n"; echo "<SELECT NAME=\"items\">\n"; echo "<OPTION VALUE=\"\">Select an Item\n"; /*populate the matching category's item list here*/ while($item_row = mysql_fetch_array($item_sql)){ echo "<OPTION VALUE=\"". $item_row['id'] ."\"". (($_POST['items'] == $item_row['id']) ? (" SELECTED") : ("")) .">". $item_row['name'] ."\n"; } echo "</SELECT>\n"; echo "<input type=\"hidden\" name=\"categories\" value=\"". $_POST['categories'] ."\">\n"; echo "<input type=\"submit\" value=\">>\"><br />\n"; echo "</form>\n"; echo "</TD>"; break; default: break; } } /* *the $_POST['item'] value is set within the switch statement. *none of this below is displayed without the user first *selecting a category, then through the switch statement *selecting an item within that category. *if an item isn't set, don't display anything. *if an item has been selected, show the form for *quantity requested, and still needs */ if(isset($_POST['items']) && !empty($_POST['items'])){ echo "<form action=\"". $_SERVER['php_self'] ."?regID=". $regID ."\" method=\"post\">"; echo "<TD align=\"right\" valign=\"bottom\">\n"; echo "<B>Quantity Requested:</B> "; echo "<input type=\"text\" name=\"qty_req\" size=\"3\"><br />\n"; echo "<B>Still Needs:</B> "; echo "<input type=\"test\" name=\"still_needs\" size=\"3\">\n"; echo "</TD></TR>"; echo "<TR><TD COLSPAN=\"3\" align=\"right\">\n"; echo "<input type=\"submit\" value=\"Submit\"></TD></TR></TABLE>\n"; echo "<input type=\"hidden\" name=\"categories\" value=\"". $_POST['categories'] ."\">\n"; echo "<input type=\"hidden\" name=\"items\" value=\"". $_POST['items'] ."\">\n"; echo "</form>\n"; } ?> </TD> </TR> </TABLE> don't pay attention to the regID or anything like that. all you're doing is populating a dropdown menu from the database, then upon submitting the first dropdown, use the $_POST value to query another sql table where some column = the first $_POST dropdown menu, thus populating another dropdown menu using the results from that query, and so on and so forth, using the switch statement. you're basically just posting the the same page. also, if you have many dropdowns to be populated dynamically, you need to send the previously posted data through hidden input fields. the reason why, is because once you have posted the first form, if you post again, the previous data will be lost, so you need to pass it through a hidden input field in the very next form, as shown in the code above. Link to comment https://forums.phpfreaks.com/topic/40566-populate-second-drop-down-based-on-selection-of-first/#findComment-196296 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.