getJSON query

1

I am trying to query aax using getJSON, but it does not return the value. What am I doing wrong?

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script><script>$(document).ready(function(){$("#ver").click(function() {

        nome=$("#course").attr("value");

        $.getJSON("teste3.php", {cpf_cnpj:nome}, function(json){
            $("#cpf_cnpj").html(json[0].cpf_cnpj);
            $("#rsocial").html(json[0].cnh);

        });
    });
});
</script>
</head>

<body>


    <strong>Cliente:</strong> 
    <input type="text" name="cliente" id="course" value="00.000.000/0000-00" size="40" /> 
    <input type="button" value="ver" id="ver" />

    <p>

    <input type="text" id="cpf_cnpj" name="cpf_cnpj" value="">
    <input type="text" id="rsocial" name="rsocial" value="">



</body>
</html>

Test3.php file

<?php

    $conexao = mysql_connect('localhost', '', '') or die  ("Erro na conexão ao banco de dados.");
    mysql_select_db('',$conexao) or die ("Erro ao selecionar a base de dados.");



    $selec = "SELECT ID_Cliente, cpf_cnpj, rsocial FROM clientes WHERE cpf_cnpj = '".$_GET["cpf_cnpj"]."' ";
    $exec = mysql_query($selec, $conexao) or die(mysql_error());

    while($campos=mysql_fetch_array($exec)) {
        extract($campos);
        $Array = Array();

        $Array[] = Array("ID_Cliente" => "$ID_Cliente", "cpf_cnpj" => "$cpf_cnpj", "rsocial" => "$rsocial");

        $json_encode = json_encode($Array);
        echo $json_encode;
    }
?>
    
asked by anonymous 12.05.2015 / 01:12

1 answer

1

Test2.php file:

Replace:

nome=$("#course").attr("value");

by:

nome=$("#course").val();

Replace also:

$("#cpf_cnpj").html(json[0].cpf_cnpj);
$("#rsocial").html(json[0].cnh);

by:

$("#cpf_cnpj").val(json[0].cpf_cnpj);
$("#rsocial").val(json[0].rsocial);

Test3.php file:

I think there is a problem with character sets.

Put in the first line of code:

header("Content-Type: application/json; charset=UTF-8")

and after the connection put this line

mysql_set_charset('utf8');

Preferably, always use UTF-8.

Do not forget to save UTF-8 files without BOM.

    
12.05.2015 / 03:18