Load a combobox by selecting another combobox

Asked

Viewed 992 times

3

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

include 'relacao-cidades.php';

And just below the combobox I intend 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);
           }       
  }

2 answers

3


You have a problem with the server, which is why Javascript does not work.

You must 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 cities but rather objects that you can iterate and search for properties in Javascript later.

  • Perfect Sergio. It worked. Thank you so much for your promptness.

  • Hi Sergio. I don’t know if I would have to open another post, but now that I’ve realized it, I believe I can take advantage of this doubt. Your help is working, but now was to realize that Idcidade returns Undefined. I already checked the bank and the nomenclature, as well as the value, are correct.

-1

Very Good.. Worked Here Using Office and Department However when use in 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 idCidade, nameCidade FROM tbCidade WHERE fkEstado =" . $ufid . ""); $lines = array(); while ($Jm = mysqli_fetch_array($query)) { $lines[] = array( "city" => $Jm["city name"], "citadel " => $Jm["idCity"] ); }

echo json_encode($linhas);

} ?>

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

Browser other questions tagged

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