Colleagues.
I have a registration form from which the user when choosing the state, the city automatically appears. However, I would like to see that in the edition, the city was already selected. The code I have is this:
<div class="item form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="">Estado: <span class="required">*</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<?php echo $metodos->listarEstados($visualizar->Estados); ?>
</div>
</div>
<div class="item form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="">Cidade: <span class="required">*</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<!--<span class="carregando">Aguarde, carregando...</span>-->
<select name="Cidades" class="select2_single form-control" id="cidades">
</select>
</div>
JQuery
$(function(){
$("#estados").change(function(){
var id = $(this).val();
// var id = <?php echo $visualizar->Cidades; ?>;
$.ajax({
type:"POST",
url:"exibe-cidades.php?id="+id,
dataType:"text",
success: function(res){
$("#cidades").children(".cidades").remove();
$("#cidades").append(res);
}
});
});
});
view-cities.php
header( 'Cache-Control: no-cache' );
header( 'Content-type: application/xml; charset="utf-8"', true );
include("conexao.php");
$id = $_GET['id'];
$sql = mysqli_query($conexao, "SELECT * FROM crm_cidades WHERE estados_cod_estados='$id' ORDER BY nome");
while($row=mysqli_fetch_array($sql)){
$nome=$row['nome'];
$id=$row['cod_cidades'];
echo '<option value="'.$id.'" class="cidades">'.utf8_encode($nome).'</option>';
}