I currently have a regular HTML combobox:
<select name="nomeCombox1" id="nomeCombox1" class="select">
<option value="0" selected="selected">Selecione box1</option>
<?php
$res = odbc_exec($conexao, "Select DISTINCT desc from tab1");
while ($row = odbc_fetch_array($res)) {
echo "<option name='nomeCombox1' value='" . $row['desc'] . "'>" . $row['desc'] . "</option>";
}
?>
</select>
This combo is correctly fetching the data.
In the next step, I have a JS where I look for what was selected and play in a new combobox:
$("#nomeCombox1").change(function () {
var idCombox1 = $(this).val();
$.ajax({
dataType: 'json',
url: "js/ajaxBuscar.php",
data: {idCombox1: idCombox1},
success: function(data) {
var q = '<select name="idCombox2" id="idCombox2" class="select"> <option value="0">Escolha</option>';
for(i=0; i < data.length; i++ ){
q += '<option value=' + data[i].idCombox2 + '>' + data[i].descricao + '</option>';
}
q += '</select>';
$('#div1').html(q);
}, error: function(request, status, error) {
alert(request.responseText);
}
});
});
These codes are all 100% working. My main question would be: How can I create this Combobox2 without being via JS? That is, without having to play $('#div1').html(q)
. Is there another way?
I'm wanting another way because I have a function in JS where I would have to check this value of that second combobox and fill an input with the result of another AJAX. When I put combobox2 straight into HTML it works. What I'm guessing is that the moment the page loads, there is no such second combo, so JS can not execute correctly.