Return JSON to PHP

Asked

Viewed 1,281 times

3

I’m making a registration system, where the ZIP code search should appear as soon as the field changes. But the AJAX return is not working. How do I return JSON to PHP? PS: ZIP search is offline

<script type="text/javascript">
jQuery(function($){
   $("#cep").change(function(){
      var cep = $(this).val();
      if( cep.length <= 0 ) return;
      $.getJSON('cep.ajax.php',{cep: $(this).val(), ajax: 'true'}, function(result) {
      alert(JSON.stringify(result));
            $("input#cep").val( result.cep );
            $("input#estado").val( result.uf );
            $("input#cidade").val( result.cidade );
            $("input#bairro").val( result.bairro );
            $("input#endereco").val( result.rua );
            $("input#estado").val( result.uf );
               });
            });
         });

</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js"></script>
<label>CEP:</label><input type="text" name="cep" id="cep" /><br />
<label>Bairro:</label><input type="text" name="bairro" id="bairro" /><br />
<label>Endereço:</label><input type="text" name="endereco" id="endereco" /><br />
<label>Número:</label><input type="text" name="numero" id="numero" /><br />
<label>Complemento:</label><input type="text" name="complemento" id="complemento" /><br />
<label>Cidade:</label><input type="text" name="cidade" id="cidade" /><br />
<label>Estado:</label><input type="text" name="estado" id="estado" /><br />


//arquivo cep.ajax.php
<?php

	$cep = mysqli_real_escape_string($concep, $_REQUEST['cep'] );
    $cep5 = substr($cep,0,5);


    //define tabela
	$sql = "SELECT * FROM cep_log_index WHERE cep5='$cep5'";
	$res = mysqli_query($concep, $sql );
	while ( $row = mysqli_fetch_assoc( $res ) ) {
		    $uf	= $row['uf'];
	}
	
	//busca cep
	$sql2 = "SELECT * FROM $uf WHERE cep='$cep'";
	$res2 = mysqli_query($concep, $sql2 );
	while ( $row = mysqli_fetch_assoc( $res2 ) ) {
		    $tipo = $row['tp_logradouro'];
            $rua  =  $row['logradouro'];
            $bairro =  $row['bairro'];
            $cidade =  $row['cidade'];
            $cep = $row['cep'];
	};

$endereco = array("tipo"=>$tipo, "rua"=>$rua, "bairro"=>$bairro, "cidade"=>$cidade, "cep"=>$cep, "uf"=>$uf);

echo json_encode($endereco);

?>

2 answers

4


I found the problem. It didn’t echo because of the accentuation. Just put the encoding as below in the file cep.ajax.php and the problem was solved!

$cidade = (utf8_encode($row['cidade']));

0

Adds the header Content-Type

header('Content-Type: application/json');
echo json_encode($endereco);
  • I believe this is not the problem, work without the header and the data is brought normally.

  • He comes to call the ajax request?

  • I tried to use Cod: "Alert(JSON.stringify(result);", to check if the variable is going but nothing.. so I don’t know if it calls the request.

  • Checks if an error appears on the console and if it is going to the request

  • I put the header, and in this I can see the page cep.ajax.php is ok. On the console shows no error, and I found that if I put the zip code without the '-', at least now echoes me the return, all in 'null'.

Browser other questions tagged

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