Ajax PHP Combobox Returns nothing

Asked

Viewed 120 times

0

I can not return anything .. when I test instead of the city field and erase there it returns but with the name of the city it does not return has data limit for a CMB?

$(document).ready(function() {
    $('#carrEst').change(function(e) {
        $('#carrCid').empty();
        var id = $(this).val();
        $.post('../include/carrCid.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.cidaden + '</option>';;
            });
            $('#carrCid').html(cmb);
        }, 'json');
    });
});

PHP

if (isset($_SERVER["HTTP_X_REQUESTED_WITH"]) && 
 $_SERVER["HTTP_X_REQUESTED_WITH"] === "XMLHttpRequest") {
$conexao = mysqli_connect('localhost', 'root', '', 'rhsystem') 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 tbCidade WHERE fkEstado =" . $ufid . "");
    $linhas = array();
    while ($jm = mysqli_fetch_array($query)) {
        $linhas[] = array(
            "cidadeid " => $jm["idCidade"],
            "cidaden" => $jm["cidade"],
        );
    }

    echo json_encode($linhas);
 }

}

HTML

<!DOCTYPE html>

TODO Supply a title

    <div class="row">  
        <div class="item form-group">
            <label id="estado" class="control-label col-md-3 col-sm-3 col-xs-12"  for="textarea">Estado <span class="required">*</span>
            </label>
            <div class="col-md-6 col-sm-2 col-xs-12">
                <select id="carrEst" >
                </select>
            </div>
        </div>

        <div class="item form-group">
            <label  id="cidade" class="control-label col-md-3 col-sm-3 col-xs-12" for="textarea">Cidade <span class="required">*</span>
            </label>
            <div class="col-md-6 col-sm-2 col-xs-12">
                <select id="carrCid" >
                </select>
            </div>
        </div>
</body>

Code to save to Javascript database

  $("document").ready(function () {

  $("#btngravar").click(function () {

    $.post("../include/cadfunc.php", {oper: "1", nome: $("#txtnome").val(),cpf: $("#txtcpf").val(),
        endereco: $("#txtendereco").val(),  cep: $("#txtcep").val(),complemento: $("#txtcomplemento").val(),bairro: $("#txtbairro").val(),
        dtNasci: $("#dtaNascimento").val(), sexo: $("#cmbSexo").val(), email: $("#txtemail").val(),dtaAdm: $("#dtaAdmissao").val(), UF: $("#carrDpto").val(),       
        cidade: $("#carrCid").val(), dpto: $("#carrDpto").val(), cargo: $("#carrCarg").val(), salario: $("#txtsalario").val(), status: $("#carrStatus").val()} ,function (data) {
        var items = [];
        i = 0;

        alert(data.tipo);

    });

});

});

PHP code

<?php
  include "conexao.php";

$oper = $_POST["oper"];

switch ($oper) {
 case 1:  
inserir($_POST["nome"],$_POST["cpf"],$_POST["endereco"],$_POST["cep"],
$_POST["complemento"],$_POST["bairro"],$_POST["dtNasci"],$_POST["sexo"],  
$_POST["email"],$_POST["dtaAdm"],$_POST["UF"],$_POST["cidade"],
$_POST["dpto"],$ _POST["cargo"],$_POST["salario"],$_POST["status"],$conn);
    break;

default:
    $data = '{"tipo": "sucesso"}';
    echo $data;
 }

 function inserir($nome, 

 $cpf,$endereco,$cep,$complemento,$bairro,$dtNasci,$sexo,$email,$dtaAdm,
 $UF,$cidade,$dpto,$cargo,$salario,$status,$conn) {
        $sql = "CALL InserirFunc ( '".$nome."', '".$cpf."', '".$endereco."', 

    '".$cep."', '".$complemento."', '".$bairro."', '".$dtNasci. "',
 '".$sexo."',  '".$email."',' ".$dtaAdm."', ".$UF.", ".$cidade.", ".$dpto.", 
 ".$cargo.", ".$salario.", ".$status.")";

        mysqli_query($conn,$sql);



    $data = '{"tipo": "sucesso"}';

    echo $data ;

  }


?>
  • 2

    Hello Somdesp, could you post the codes instead of images? makes it easy for the person to test and help you.

  • ready .. first time I do a post here was not managing to do by code now I managed to

  • Is there a city with an emphasis on the case?

  • yes. several cities

  • Try to put it like this: "cidaden" => utf8_encode($jm["cidade"])

  • Now it worked. Thank you very much

Show 1 more comment

1 answer

0


I was able to find a solution

I changed the Javascript

$(function(){
$('#carrDpto').change(function(){
    if( $(this).val() ) {
        $('#carrCarg').hide();
        $('.carregando').show();
        $.getJSON('../include/carrCarg.php?search=',{carrDpto: $(this).val(), ajax: 'true'}, function(j){
            var options = '<option value=""></option>'; 
            for (var i = 0; i < j.length; i++) {
                options += '<option value="' + j[i].carrCarg + '">' + j[i].nome + '</option>';
            }   
            $('#carrCarg').html(options).show();
            $('.carregando').hide();
        });
    } else {
        $('#carrCarg').html('<option value="">-- Escolha um Departamento --</option>');
    }
});
});

And PHP

header('Cache-Control: no-cache');
header('Content-type: application/xml; charset="utf-8"', true);

 include 'conexao.php';



 $carrEst = mysqli_real_escape_string($conn, $_REQUEST['carrEst']);

 $cidades = array();

  $query = mysqli_query($conn, "SELECT idCidade,nomeCidade FROM tbCidade 
 WHERE fkEstado =  " . $carrEst . " ");


while ($row = mysqli_fetch_assoc($query)) {
$cidades[] = array(
    'carrCid' => $row['idCidade'],
    'nome' => utf8_encode($row['nomeCidade']),
    );
}

echo( json_encode($cidades) );

Browser other questions tagged

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