Query getJSON

Asked

Viewed 349 times

1

I am trying to make an ajax query using getJSON, but it does not return the value. What I am 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>

File teste3.php

<?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;
    }
?>
  • Have you noticed that the file name for query is "teste3.php"?

  • @Rodrigospeller, yes I put wrong in the post.

  • When I make the query directly in the browser appears the result. teste3.php? cpf_cnpj=00.000.000/0000-00 [{"ID_Cliente":"1","cpf_cnpj":"00.000.000\/0000-00","rsocial":"Teste ME"}]

  • It looks all right. There is some error in the browser console?

  • Strange, not being sent the VALUE, see image http://i.imgur.com/bzLtPTt.png

  • I changed my nome=$("#course").attr("value"); for nome=$('#course').val(); and now it is passing the value, but not yet returns the value of the search.

Show 1 more comment

1 answer

1


File teste2.php:

Substitute:

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

for:

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

Also replace:

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

for:

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

File teste3.php:

I think there’s a problem with the character sets.

Put it 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.

Don’t forget to save the files in UTF-8 format without GOOD.

  • Hi Rodrigo, I did as you said and it still didn’t work. In File teste2.php I returned the <?php header("Content-Type: application/json; charset=UTF-8"); ?>, but instead of appearing the form appears the code

  • In mine it worked, I’ll redo here from scratch and see what’s different.

  • Thank you very much!

  • You changed the name of the Test file?

  • It remains the same. TESTE2.PHP is the one with the html form. TESTE3.PHP is the one that searches the database.

  • Dump the bank if it’s not too big.

  • You will put header("Content-Type: application/json; charset=UTF-8") in the archive teste3.php.

  • I’m on chat, I put the BD there

Show 4 more comments

Browser other questions tagged

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