I am setting up dynamic drop-down lists: selecting in the selection category # informs the contents of the select # subdirectory.
this is the html for select # category:
<select name="category" id="category">
<option>Click Here to choose a category</option>
<?php foreach ($categories as $key => $category) { ?>
<option value="<?php echo $key ?>"><?php echo $category ?></option>
<?php } ?>
</select>
<select name="subcat" id="subcat">
<option><-- First Choose a Category</option>
</select>
this is a jQuery statement:
$(document).ready(function(){
$("select#category").change(function(){
$("select#subcat").load("subcats.php",{ id: $(this).val(), ajax: 'true'});
});
});
this is subcats.php:
<?php
include("config.php") ;
$options = '<option value="">Choose a Sub-category</option>'."\n" ;
if(!empty($_GET["id"])) {
foreach ($categories as $key => $category) {
if ($_GET["id"] == $key) {
$subcats = file('subcat/'.$key.'.dat', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) ;
natsort($subcats);
foreach ($subcats as $subcat) {
list ($subname,$subkey ) =explode ("\t", $subcat) ;
$options .= '<option value="'.$subkey.'">'.$subname.'</option>'."\n" ;
}
echo $options ;
}
}
} else {
echo '<option value=""><-- Choose a Category First</option>
<option value="">Foo Bar</option>'."\n" ;
}
?>
I understand that it id: $(this).val()provides the selected option from select#categoryfor use in a php document; I'm here? Indeed, if I read the documents correctly, I donβt even need a bit. The , { id: $(this).val(), ajax: 'true'}
ajax request .loadworks because the error checking menu items are displayed in select#subcats.
However, that all that appears: the variable does not seem to pass ...
Is there any help Thanks in advance!
source
share