If I understood your question correctly, then something like this might do the trick.
// html markup for the selects
<select name="main_cat">
<option value="A">A</option>
<option value="B">B</option>
</select>
<select name="sub_cat_for_A" class="sub-cat-select hidden">
<option value="C">C</option>
<option value="D">D</option>
<option value="E">E</option>
</select>
<select name="sub_cat_for_B" class="sub-cat-select hidden">
<option value="F">F</option>
<option value="G">G</option>
<option value="H">H</option>
</select>
If you change your mind and want to use a taxonomy from WordPress, you can change the markup to this,
<?php
$main_categories = array();
$sub_categories = array();
$cats = get_terms(array(
'taxonomy' => 'your_taxonomy',
));
if ( $cats && ! is_wp_error( $cats ) ) {
foreach ( $cats as $cat ) {
if ( $cat->parent ) {
$sub_categories[$cat->parent][$cat->term_id] = $cat->name;
} else {
$main_categories[$cat->term_id] = $cat->name;
}
}
}
?>
<select name="main_cat">
<?php foreach ( $main_categories as $id => $name ) : ?>
<option value="<?php echo $id; ?>"><?php echo $name; ?></option>
<?php endforeach; ?>
</select>
<?php foreach ( $sub_categories as $main_cat_id => $sub_category_options ) : ?>
<select name="<?php echo "sub_cat_for_{$main_cat_id}" ?>" class="sub-cat-select hidden">
<?php foreach ( $sub_category_options as $id => $name ) : ?>
<option value="<?php echo $id; ?>"><?php echo $name; ?></option>
<?php endforeach; ?>
</select>
<?php endforeach; ?>
Then you just need some jQuery (vanilla javascript could be used also) to watch changes on the main select and to show the related sub-select.
jQuery(document).on('ready',function($){
$('select[name="main_cat"]').on('change',function(event){
var selectedMainCatId = $(this).value();
// hide other sub cat selects
$('.sub-cat-select').addClass('hidden');
// show correct sub cat select
$('select[name="sub_cat_for_'+selectedMainCatId+'"]').removeClass('hidden');
});
});
There might be room for improvement on the jQuery example, but I’m sure you’ll get the basic principle from it.