3
When returning the values in the combobox, it is returning as Undefined:
include 'relacao-cidades.php';
And just below the combobox I intend to load the states according to the selected city:
<select name="Estados" id="CmbCidade" class="form-control">
</select>
And Jquery that is not working:
<script type="text/javascript">
$(document).ready(function() {
$('#CmbUF').change(function(e) {
$('#CmbCidade').empty();
var id = $(this).val();
$.post('listar-cidades.php', {ufid:id}, function(data){
var cmb = '<option value="">Selecione a Cidade</option>';
$.each(data, function (index, value){
cmb = cmb + '<option value="' + value.cidadeid + '">' + value.cidade + '</option>';;
});
$('#CmbCidade').html(cmb);
}, 'json');
});
});
</script>
PHP:
if (isset($_SERVER["HTTP_X_REQUESTED_WITH"]) && $_SERVER["HTTP_X_REQUESTED_WITH"] === "XMLHttpRequest"){
$conexao = mysqli_connect('127.0.0.1','root','','tabela') or die(mysqli_error($conexao));
$ufid = filter_input(INPUT_POST, 'ufid', FILTER_SANITIZE_NUMBER_INT);
//$ufid = 48;
if ($ufid){
$query = mysqli_query($conexao, "SELECT IdCidade, Cidade FROM cidades WHERE IdCidade =".$ufid."");
$linhas = array();
while($jm = mysqli_fetch_array($query)){
$linhas[] = $jm["Cidade"];
}
echo json_encode($linhas);
}
}
Perfect Sergio. It worked. Thank you so much for your promptness.
– user24136
Hi Sergio. I don’t know if I would have to open another post, but now that I’ve realized it, I believe I can take advantage of this doubt. Your help is working, but now was to realize that Idcidade returns Undefined. I already checked the bank and the nomenclature, as well as the value, are correct.
– user24136