Load a combobox by selecting another combobox

3

When returning the values in the combobox, it is returning as undefined:

include 'relacao-cidades.php';

And just below the combobox that I want to load the states according to the selected city:

 <select name="Estados" id="CmbCidade" class="form-control">

 </select>

And Jquery that is not working:

<script type="text/javascript">
    $(document).ready(function() {
        $('#CmbUF').change(function(e) {
            $('#CmbCidade').empty();
            var id = $(this).val();
            $.post('listar-cidades.php', {ufid:id}, function(data){
                var cmb = '<option value="">Selecione a Cidade</option>';
                $.each(data, function (index, value){
                    cmb = cmb + '<option value="' + value.cidadeid + '">' + value.cidade + '</option>';;
                });
                $('#CmbCidade').html(cmb);
            }, 'json');
        });
    });
</script>

PHP:

if (isset($_SERVER["HTTP_X_REQUESTED_WITH"]) && $_SERVER["HTTP_X_REQUESTED_WITH"] === "XMLHttpRequest"){
       $conexao = mysqli_connect('127.0.0.1','root','','tabela') or die(mysqli_error($conexao));
        $ufid = filter_input(INPUT_POST, 'ufid', FILTER_SANITIZE_NUMBER_INT);
//$ufid = 48;
        if ($ufid){
            $query = mysqli_query($conexao, "SELECT IdCidade, Cidade FROM cidades WHERE IdCidade =".$ufid."");
            $linhas = array();
           while($jm = mysqli_fetch_array($query)){
                $linhas[] = $jm["Cidade"];
            }

            echo json_encode($linhas);
           }       
  }
    
asked by anonymous 09.02.2016 / 19:11

2 answers

3

You have a server problem, which is why JavaScript does not work.

You should change

$linhas[] = $jm["Cidade"];

for

$linhas[] = array(
    "cidade" => $jm["Cidade"],
    "cidadeid " => $jm["IdCidade"]
);

For it to pass not a simple array with the name of the cities but rather objects that you can iterate and go to search properties in JavaScript later.

    
09.02.2016 / 20:41
-1

Very Good .. Worked Here Using Job Title and Department But when I use the city is not returning anything

<?php

include "connection.php";

$ ufid = filter_input (INPUT_POST, 'ufid', FILTER_SANITIZE_NUMBER_INT); // $ ufid = 48; if ($ ufid) {     $ query = mysqli_query ($ conn, "SELECT idCITY, nameCITY FROM tbCITY WHERE fkState=". $ ufid. "");     $ rows = array ();     while ($ jm = mysqli_fetch_array ($ query)) {         $ rows [] = array (             "city" = > $ jm ["nameCity"],             "ciudadid" = > $ jm ["idCity"]         );     }

echo json_encode($linhas);

} ? >

$ (document) .ready (function () {     $ ('# carrEst'). change (function (e) {         $ ('# carrCid').         var id = $ (this) .val ();         $ .post ('../ include / carrCid.php', {ufid: id}, function (data) {             var cmb = 'Select the City';             $ .each (data, function (index, value) {                 cmb = cmb + '' + value.city + '' ;;             });                 $ ('# carrCid'). html (cmb);             json ');         });     });

    
06.06.2017 / 19:06