0
I want to select where, in a combobox, I select the state and open a new combobox with the respective cities of this state.
I have it divided into two files:
php states.
//conexao com o banco...
$rs = mysql_query("select distinct loc_uf from local ORDER BY loc_uf ASC");
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#estado').change(function(){
$('#cidade').load('listaCidades.php?estado='+$('#estado').val());
});
});
</script>
</head>
<body>
<label>Estado:</label>
<select name="estado" id="estado">
<?php while($reg = mysql_fetch_object($rs)): ?>
<option value="<?php echo $reg->id_local ?>"><?php echo $reg->loc_uf?></option>
<?php endwhile;
?>
</select>
<br /><br />
<div id="cidade"></div>
</body>
</html>
With this all states are being listed within the combobox. Now the file lists Cidades.php
<?php
//conexao...
$estado = $_GET['estado'];
$rs = mysql_query("select loc_cidade from local where loc_uf ='".$estado."' ORDER BY loc_cidade");
echo "<label>Cidade: </label><select name='cidade'>";
while($reg = mysql_fetch_object($rs)){
echo "<option value='$reg->loc_cidade'>$reg->loc_cidade</option>";
}
echo "</select>";
?>
But the combobox is not being filled correctly.
What am I doing wrong? How do I capture the selected value in the combo and step as parameter?
bank table: local fields: id_local, loc_city, loc_uf
That is, I have no UF id, only of each city that respectively has a UF.
face you are not using the variable
$estado
in your query, but I didn’t quite understand the layout of your tables. ??– periotto
the table is one. table LOCAL. there has id_local, loc_city and loc_uf. ie, ID 1 has a city and its state.
– PHP developer
Dude, I don’t think this is the best way to do this research. I used a bank with various tables - state, city, neighborhood and depending on the country situation. The way q is doing this table will look monstrous, giant even.
Se quiser pode usar a api dos correios, vc passa o cep e eles te retornam o endereco completo
– periotto
It is... soon I noticed it too. However the table is already ready this way and filled. It has how to do this way using this table?
– PHP developer
adding the state in query worked?
– periotto
Below the line
$estado = $_GET['estado'];
typoecho "select loc_cidade from local where loc_uf ='".$estado."' ORDER BY loc_cidade"; die();
, then copies the text and plays straight into phpmyadmin, and checks to see if the waiting returns.– henriquedpereira
Not picking up the selected state: Return this: select loc_cidade from local Where loc_uf ='' ORDER BY loc_cidade
– PHP developer
http://answall.com/questions/99107/listar-estados-cidades-e-bairros-em-formul%C3%A1rio-de-cadastro/99133#99133
– marcusagm