Ajax does not load

Asked

Viewed 104 times

0

I’m trying to fill 2 combobox (State), (City) with the database return, the first of states I got, the states are shown and then I take their UF to query municipalities, however my ajax does not seem to call the php file.

HTML

<div class="input-field col s4 m3">
        <select name="estado" id="id_estados" class="browser-default" >
            <option value=" " selected="selected">Escolha o estado:</option>
            <?php include_once("/Chamadas/listarEstados.php"); ?>
        </select>
    </div>
    <div class="input-field col s8 m5">
        <select name="municipios" id="id_municipios" class="browser-default" >
            <option value="" disabled selected>Cidade</option>
            <script>
                $("#id_estados").on("change", function()
                {
                    var idEstado = $("#id_estados").val();
                    var url = '/Chamadas/listarMunicipios.php';
                    alert(idEstado);
                    $.ajax({
                        url:url,    
                        type: 'POST',
                        data: {ufEstado : idEstado},
                        beforeSend: function(){
                        $("#id_municipios").html("Carregando..");
                        },
                        success: function(data)
                        {
                            $("#id_municipios").html(data);
                        },
                        error: function(data)
                        {


                            $("#id_municipios").html("Houve um erro ao carregar !");
                        }
                    });

                });
            </script>
        </select>
    </div>

PHP

<?php   
    include_once("DAO/dalLocalizacao.php");

    $dalLocalizacao = new dalLocalizacao();
    $municipios = $dalLocalizacao->listarMunicipio($_POST['ufEstado']);

    foreach ($municipios as $municipio) 
    {
        $municiopUTF = utf8_encode($municipio['Nome']);
        echo '<option>'.$municiopUTF.'</option>';# code...
    }
?>

PHP2

public function listarMunicipio($uf)
    {

     $query = "SELECT * FROM Municipio WHERE Uf='".$uf."'";
     $municipios = $this->conexao->query($query) or die(mysql_error());
     return $municipios;

    }

Folder structure

inserir a descrição da imagem aqui

  • Put a.log(data) console in the Success and see what it shows.

  • Just so you know, the function listarMunicipio($uf) is subject to SQL injection attacks.

No answers

Browser other questions tagged

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