I've searched high and low for the solution to my question. No luck. I'm building a book library with search capabilities. I have categories and sub categories. I'm looking to have 2 dropdowns on my page - one for the categories and a second that changes with the selection of the first dropdown. Is there a way to do this without using javascript and without having two separate pages? Please help. THNX
Dependent 2nd dropdown - NO JAVASCRIPT
Here's the way I skinned this cat. The first time the script is run only one drop down list is displayed. When the user selects from the drop down list, the script runs again but now the second drop down list is displayed. This works for me because the contents of the second list are determined by the item selected in the first list.
Hank
+++++++++++++++ snippet ++++++++++++++
<?php
/** all_cases_deft.php ***/
...
/************ FIRST DROP DOWN LIST *************/
/ First I display all the names of the defendants ****/
print("<form method=post action=\"/earthquake/all_cases_deft.php\">\n");
print("<table width=400 border=0><tr align=center><td>\n");
print("<select size=1 name=deft>\n");
$num_defts = pg_numrows($sel_defts);
for($i=0; $i<$num_defts; $i++) {
$arr1 = pg_fetch_array($sel_defts, $i);
if ($deft == trim($arr1["deft"])) {
printf("<option value=\"%s\" selected>%s\n", trim($arr1["deft"]), trim($arr1["deft"]));
}
else {
printf("<option value=\"%s\">%s\n", trim($arr1["deft"]), trim($arr1["deft"]));
}
} / END FOR LOOP /
print("</select></td></tr></table>\n");
print("<input type=submit value=\"Select Defendant\"></form>\n");
print("<br><font size=3 color=990000>Select a Defendant from the drop down list and click the Select Defendant button</font>\n");
/****************** SECOND DROP DOWN LIST **********************/
/ If a defendant name was passed to this script (which calls itself) then
I display all the case numbers for this particular defendant ******/
if (strlen($deft) > 0) {
print("<form method=post action=\"/earthquake/process_srch_case.php\">\n");
$sel_case = pg_exec($db_conn, "SELECT case_nbr FROM defendant where deft = '$deft' order by case_nbr");
$num_cases = pg_numrows($sel_case);
printf("<font size=5>%d case(s) for %s</font>\n", $num_cases, $deft);
print("<table width=400 border=0><tr align=center><td>\n");
print("<select size=1 name=case_nbr>\n");
for($i=0; $i<$num_cases; $i++) {
$arr1 = pg_fetch_array($sel_case, $i);
printf("<option value=\"%s\">%s\n", trim($arr1["case_nbr"]), trim($arr1["case_nbr"]));
} / END FOR LOOP /
print("</select></td></tr></table>\n");
print("<input type=submit value=\"View Case\"></form>\n");
print("<p><font size=3 color=990000>Select a Case Number to view or go up and select a different Defendant</font>\n");
}
...
?>
you should chat with the person who started this thread, he's dealing with the same issue as you are. read that thread, it might help you also.
you can do this with one form that does a request to its own page. it would have 2 select widgets, but one would be hidden until the user makes a selection from the first one.
<?
if (isset($category) && isset($otherCriteria) &&
$category != "" && $otherCriteria != "")
{
// do search stuff here,
// the user has chosen from both dropdowns
}
?>
<form method="get" action="<?=$REQUEST_URI?>">
Selection #1
<select name="category" onChange="this.form.submit()">
<option value="biographies" <?=($category== "biographies" ? "selected" : "")?> >Biographies
<option value="comics" <?=($category== "biographies" ? "selected" : "")?> >Comic Books
</select>
<?
if (isset($category) && $category!= "")
{
// Probably have to do some work here to find the
// options for this second dropdown box
$otherOptions = someFunction($category);
?>
<p>
Selection #2
<select name="otherCriteria" onChange="this.form.submit()">
<option value="">Choose:
<?= $otherOptions ?>
</select>
<?
}
?>
</form>
Thanks to Hank and merkinmuffley for the suggestions. It works wonderfully. I had to do a bit of tweaking with the code and how to get the variable to send to the next, but it works.