Page does not take cities from the database, only the state

Asked

Viewed 28 times

-1

I have a page that selects the state in the database and after selected, it chooses the cities of that state, but can not get the cities, is only taking the state.

Here is the main code that makes the selection:

    <form method="post" action="../App/Controller/insertFinalizado.php" class="colorlib-form">
                <div class="row">
                    <div class="col-md-7">
                        
                            <h2>Endereço para entrega</h2>
                            <div class="row">
                               <div class="col-md-12">
                                  <div class="form-group">
                                    <label for="country">Estado</label>
                                    <div class="form-field">
                                        <i class="icon icon-arrow-down3"></i>
                                        <select class="form-control" name="id_estado" id="id_estado" required>
                                            <option value=""> Selecione...</option>
                                             
                                                <?php foreach( $resultado_estados as $row ) { 
                                                    echo '<option value="'.$row['Uf'].'">'.$row['Nome'].'</option>';
                                                } ?>

                                        </select>                            
                                    </div>
                                </div>
                                  <div class="form-group">
                                    <label for="id_cidade">Municipio</label>
                                     <div class="form-field">
                                        <i class="icon icon-arrow-down3"></i>
                                        <select class="form-control" name="id_cidade" id="id_cidade" required>
                                            <option value="#id_cidade">Selecione...</option>
                                            
                                        </select>                            
                                     </div>

                                    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
                                    <script type="text/javascript">
                                        $('#id_estado').change(function (){
                                            var valor = document.getElementById("id_estado").value;
                                            $.get("exibe_cidade.php?search=" + valor, function (data) {
                                                $("#id_cidade").find("option").remove();
                                                $('#id_cidade').append(data);
                                            });
                                        });
                                    </script>
                                  </div>
                               </div>
                            </div>
                        
                    </div>
     </form>

Here is the page exibe_cidade.php which should select the cities of a given state:

<?php

    include_once '../DataBase/conexao.php';

    session_start();
    $Uf = isset($_GET['search']) ? $_GET['search'] : 0;

    $conn = new Conexao();
    $conn = $conn->conexao();

    $stmt = $conn->prepare('SELECT * FROM municipio WHERE estado_Uf = "'.$Uf.'" ORDER BY Nome');
    
    $stmt->execute();
    $resultado_cidades = $stmt->fetchAll();
    
    foreach( $resultado_cidades as $row_cidade ) { 
        echo '<option value="'.$row_cidade['Id'].'">'.$row_cidade['Nome'].'</option>';
    }

?>
  • Please clarify your specific problem or provide Additional Details to Highlight Exactly what you need. As it’s Currently Written, it’s hard to Tell Exactly what you’re asking.

  • How is your file conexao.php? Another thing, it’s not good practice for you to create an object and then reassign that reference to another type of object. You did this by calling $conn = $conn->conexao(), in the case, conn was already an instance of Conexao.

No answers

Browser other questions tagged

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