I already have the first HTML select populated by the result of the first query.
Now what I need or want to do is put the results from the second query on a Json file (maybe) or an XML (don't care) and then using javascript (not jquery) populate the second HTML select based on the first HTML select option selected.
This is my current code:
<?php
# Get all brands id and brand name to populate brands HTML select 1
$q1=mysqli_query($conect,"SELECT id, brand FROM brands ORDER BY brand ASC");
$brand_select="";
while($brand=mysqli_fetch_assoc($q1)){
$brand_select=$brand_select.'<option value="'. $brand['id'] .'">'. $brand['brand'] .'</option>';
}
# Get all models id and model name to populate models HTML select based on the brands ID from html select 1
$q2=mysqli_query($conect,"SELECT id, brand_id, model FROM models ORDER BY model ASC");
$models_select="";
while($model=mysqli_fetch_assoc($q2)){
$models_select=$models_select.'<option value="'. $model['id'] .'">'. $model['modelo'] .'</option>';
}
?>
<select name="brand" id="brands" class="s1" onchange="populate(this.id,'modelos')">
<option value="">Select Brand</option>
<?= $brand_select ?>
</select>
<br>
<select name="models" id="models" class="s1">
<option value="">Select Model</option>
<?= $models_select ?>
</select>
The id from the brands table is related to the brand_id on the models table for relational purposes.
The brands HTML select are displaying the brands just perfect as it should and the options as is, are showing the full list of models of all brands.
I'm sure that an onChange event should be used on the first HTML select, that is as far as I can get without Javascript.
Of course I've looked other Similar Questions but can't find one that matches my idea about this.
Any suggestion from You the real genius :-)
populate()function)