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"
?– Rodrigo Speller
@Rodrigospeller, yes I put wrong in the post.
– Tiago
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"}]
– Tiago
It looks all right. There is some error in the browser console?
– bfavaretto
Strange, not being sent the VALUE, see image http://i.imgur.com/bzLtPTt.png
– Tiago
I changed my
nome=$("#course").attr("value");
fornome=$('#course').val();
and now it is passing the value, but not yet returns the value of the search.– Tiago