Forms
|
|
|
|
<?php ### this neat javascript function I adapted from Leon Atkinson's code updates the second dropdown box
### (subcategory) depending on what is selected in the first (category).
### this function obviously needs to be contained in a form.
###
### array_key_ID is an array of the unique identifiers of the subcategory
### array_cat is the array of categories
### array_subcat is the array of the subcategories
### num_in_array is the total number of items in the array
### distinct_cats is the number of unique items in the category field
### MaxItems is the maximum number of items that can occur in the subcategory dropdown box
###
### Works in IE 4+ and Mozilla 0.9.9+
### Not currently working in Opera (6.02)
function dynamicboxes ($array_key_ID, $array_cat, $array_subcat, $num_in_array, $distinct_cats, $MaxItems ){
print "<SCRIPT Language="JavaScript">n";
print "function UpdateSubCat()n";
print "{n";
print "tvar SubCat = document.choose_category.subcat.options.length;n";
print "twhile(SubCat > 0)n";
print "t{n";
print "ttSubCat--;n";
print "ttdocument.choose_category.subcat.options[SubCat].text = '';n";
print "ttdocument.choose_category.subcat.options[SubCat].value = '';n";
print "t}nn";
print "tdocument.choose_category.subcat.options[0].selected = true;nn";
print "tvar CatSelected = 0;n";
print "twhile(document.choose_category.cat.options[CatSelected].selected==false) CatSelected++;nn";
$StoreCat = "";
$SubCatCount = 0;
$RowCount = 0;
while($RowCount < $num_in_array)
{
$item_key_ID = $array_key_ID[$RowCount];
$item_Cat = $array_cat[$RowCount];
$item_SubCat = $array_subcat[$RowCount];
if($StoreCat != $item_Cat)
{
if($StoreCat != "")
{
print "t}n";
}
print "tif(document.choose_category.cat.options[CatSelected].value == '$item_Cat')n";
print "t{n";
print "ttdocument.choose_category.subcat.options[0].value = 0;n";
print "ttdocument.choose_category.subcat.options[0].text = 'Choose a subcategory';nn";
$StoreCat = $item_Cat;
$SubCatCount=1;
}
print "ttdocument.choose_category.subcat.options[$SubCatCount].value = '$item_key_ID';n";
print "ttdocument.choose_category.subcat.options[$SubCatCount].text = '$item_SubCat';nn";
$SubCatCount++;
$RowCount++;
}
print "t}n";
print "}n";
print "</SCRIPT>nn";
print "<SELECT NAME=cat onChange="UpdateSubCat();">n";
print "<OPTION VALUE=0>Choose category";
$RowCount = 0;
while($RowCount < count($distinct_cats))
{
print "<OPTION VALUE="";
print $distinct_cats[$RowCount];
print "">";
print $distinct_cats[$RowCount];
print "n";
$RowCount++;
}
print "</SELECT>n";
print "<BR>";
print "<SELECT NAME=subcat >n";
$RowCount = 0;
while($RowCount <= $MaxItems)
{
print "<OPTION VALUE=$RowCount>";
print "Subcategoryn";
$RowCount++;
}
print "</SELECT>";
print "<SCRIPT Language="JavaScript">UpdateSubCat();</SCRIPT>n";
} ?>
|
|
|
Usage Example
|
<?php
### this neat javascript function from Leon Atkinson updates the second dropdown box
### (subcategory) depending on what is selected in the first (category).
### this function obviously needs to be contained in a form.
###
### array_key_ID is an array of the unique identifiers of the subcategory
### array_cat is the array of categories
### array_subcat is the array of the subcategories
### num_in_array is the total number of items in the array
### distinct_cats is the number of unique items in the category field
### MaxItems is the maximum number of items that can occur in the subcategory dropdown box
###
### Works in IE 4+ and Mozilla 0.9.9+
### Not currently working in Opera (6.02)
function dynamicboxes ($array_key_ID, $array_cat, $array_subcat, $num_in_array, $distinct_cats, $MaxItems ){
print "<SCRIPT Language="JavaScript">n";
print "function UpdateSubCat()n";
print "{n";
print "tvar SubCat = document.choose_category.subcat.options.length;n";
print "twhile(SubCat > 0)n";
print "t{n";
print "ttSubCat--;n";
print "ttdocument.choose_category.subcat.options[SubCat].text = '';n";
print "ttdocument.choose_category.subcat.options[SubCat].value = '';n";
print "t}nn";
print "tdocument.choose_category.subcat.options[0].selected = true;nn";
print "tvar CatSelected = 0;n";
print "twhile(document.choose_category.cat.options[CatSelected].selected==false) CatSelected++;nn";
$StoreCat = "";
$SubCatCount = 0;
$RowCount = 0;
while($RowCount < $num_in_array)
{
$item_key_ID = $array_key_ID[$RowCount];
$item_Cat = $array_cat[$RowCount];
$item_SubCat = $array_subcat[$RowCount];
if($StoreCat != $item_Cat)
{
if($StoreCat != "")
{
print "t}n";
}
print "tif(document.choose_category.cat.options[CatSelected].value == '$item_Cat')n";
print "t{n";
print "ttdocument.choose_category.subcat.options[0].value = 0;n";
print "ttdocument.choose_category.subcat.options[0].text = 'Choose a subcategory';nn";
$StoreCat = $item_Cat;
$SubCatCount=1;
}
print "ttdocument.choose_category.subcat.options[$SubCatCount].value = '$item_key_ID';n";
print "ttdocument.choose_category.subcat.options[$SubCatCount].text = '$item_SubCat';nn";
$SubCatCount++;
$RowCount++;
}
print "t}n";
print "}n";
print "</SCRIPT>nn";
print "<SELECT NAME=cat onChange="UpdateSubCat();">n";
print "<OPTION VALUE=0>Choose category";
$RowCount = 0;
while($RowCount < count($distinct_cats))
{
print "<OPTION VALUE="";
print $distinct_cats[$RowCount];
print "">";
print $distinct_cats[$RowCount];
print "n";
$RowCount++;
}
print "</SELECT>n";
print "<BR>";
print "<SELECT NAME=subcat >n";
$RowCount = 0;
while($RowCount <= $MaxItems)
{
print "<OPTION VALUE=$RowCount>";
print "Subcategoryn";
$RowCount++;
}
print "</SELECT>";
print "<SCRIPT Language="JavaScript">UpdateSubCat();</SCRIPT>n";
}
$Akey_ID = array("1","2","3","4","5"); $Acat = array("Fruit","Fruit","Fruit","Car","Rodent"); $Asubcat = array("Apples", "Bananas", "Oranges", "Rolls Royce", "Gerbil"); $num_in_array = 5; $Adistinct_cats = array("Fruit","Car","Rodent"); $max_items = 3;
print"<HTML>";
#### Collect the submitted values, then print them: if (count($HTTP_POST_VARS)) {
while (list($key, $value) = each ($HTTP_POST_VARS)) {
$arr_request[$key] = $value;
}
}
if ($arr_request[cat]){print $arr_request[cat]." - ".$arr_request[subcat];}
#### Main Form
print "<FORM NAME=choose_category ACTION=$PHP_SELF METHOD=POST>";
dynamicboxes($Akey_ID, $Acat, $Asubcat, $num_in_array, $Adistinct_cats, $max_items );
print "<BR><BR><INPUT TYPE="submit"><BR>n";
print "</FORM>";
print"</HTML>";
?>
|
|
|
Rate This Script
|
|
|
|