PHP Ajax - state combobox - cities

Asked

Viewed 355 times

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. ??

  • the table is one. table LOCAL. there has id_local, loc_city and loc_uf. ie, ID 1 has a city and its state.

  • 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

  • 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?

  • adding the state in query worked?

  • Below the line $estado = $_GET['estado']; typo echo "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.

  • Not picking up the selected state: Return this: select loc_cidade from local Where loc_uf ='' ORDER BY loc_cidade

  • http://answall.com/questions/99107/listar-estados-cidades-e-bairros-em-formul%C3%A1rio-de-cadastro/99133#99133

Show 3 more comments

1 answer

1

In the.php states file, the value of each option is the id_local field of the bd, ie to find out what was the selected state and return the cities will have to put:

WHERE id_local = $estado

and not loc_uf, as it is in the file listings Cidades.php.

A small addendum, the mysql_* functions are obsolete and should be removed from php soon, it is recommended to use mysqli_* or PDO.

  • What happens is that the option selected in the combobox is not being passed to the $state variable in lists.php .

Browser other questions tagged

You are not signed in. Login or sign up in order to post.