Hierarchic menus using Javascript and PHP?

From: Date: Mon, 10 Sep 2001 20:06:55 +0000
Subject: Hierarchic menus using Javascript and PHP?
Groups: php.general 
Request: Send a blank email to php-general+get-66808@lists.php.net to get a copy of this message
On my site I have a page that takes input into a MySQL database (though this isn't a db question). Each entry has a category and a subcategory entry, which should come from predefined lists and are therefore instituted as pull-down lists. The category list is in a PHP array which feeds a SELECT pull-down list. The subcategory choices should depend upon what category is selected. In order to ease user input, I've used a javascript function to cause the subcategory pulldown to depend upon the category selection. Thus if the category is Animals, the subcategory SELECT would show "dog", "cat", "fish" - but if the category were "Colors" the possible subcategories might be "Red", "Green", "Yellow", "Puce". On the initial entry - a blank record - this all works fine. But if I go back in to modify a record I run into trouble, as I can't make the page reflect the stored value of the subcategory within the pull-down list (i.e. as the SELECTED item). I use the following PHP code to display the $categories array - and if there is a stored value ( $formValues["category"] ) it displays that selection as "SELECTED" - which is exactly right. printf("<TR><TD>Category </TD>"); printf("<TD><SELECT NAME=category ONCHANGE=\"updateCats()\">\n"); printf("<OPTION VALUE=0> === Select One === </OPTION>\n"); $i=0; while ($i < count($categories)) {
    printf("<OPTION VALUE=%s %s>%s</OPTION>\n",
      $categories[$i],
      $categories[$i] == $formValues["category"] ? "SELECTED" : "",
      $categories[$i]);
    $i++;
} printf("</SELECT></TD></TR>); But when it comes to the Sub-Category the list that feeds the SELECT list is not in an array, but is coming from the JavaScript function like: if(subCategoryIndex == 1) // Colors
    document.search.subcategory.options[1] = new Option('Red');
    document.search.subcategory.options[1].value = "red";
    document.search.subcategory.options[2] = new Option('Blue');
    document.search.subcategory.options[2].value = "blue";
    ...etc...
I can't figure a way to make the 'Update' version of the page display the value of the Sub-Category in a select list (so that the user CAN update it). I can certainly have the value display, but it no long has the other possible members of the list, since it's not coming from a display. I couldn't see a way to do this (updating one form element from another) completely in PHP - else I would have. Does anyone have any ideas? Could I feed a PHP array FROM JavaScript? If I could get the subcategory selection list into an arry I think I could make this work. Or possibly use another JavaScript script inside the PHP portion of the page in order to force it to mark the appropriate item "SELECTED"? I tried this but couldn't make it work. Any help is appreciated - Thanks, Nelson

« previous php.general (#66808) next »