0
I have an application where, a Javascript I have a function that performs an Ajax call to the file . php handle. The . php handles the function buscaConta(p) like: makes a SELECT in the Mysql database (managed via XAMPP and phpMyAdmin), takes the result of this query, saves in an array and returns to another Javascript function (callback), called "formaCanvas(p)" in Json format. Only that what I get in this callback JS function is [] . What would be a correct empty array? That most of the time. Only the first time I run the application after it is loaded do I get the correct result on it: the agency, account and balance number. . That would be a correct empty array?
Only that function search engine() php, before sending the query result to formaCanvas from JS, I test this query result, and it is an array filled with values...
Why it arrives empty for the function formaCanvas() J? Where am I wrong? Javascript function containing Ajax call:
function formaQueryConta(cpf){
var numConta= jsonDtContasCliente[(document.getElementById("listaContas").value)-1];
var parms = "&cpf="+cpf+"&conta="+numConta;
ajaxCall("Persistencia.php?action=buscaConta" +parms, formaCanvas);
}
All right in the above function.
Ajaxcall function:
function ajaxCall(stringCall, callback){
var httpRequest = new XMLHttpRequest;
httpRequest.onreadystatechange = function(){
if (httpRequest.readyState === 4) {
if (httpRequest.status === 200) {
callback(httpRequest.responseText);
}
}
};
httpRequest.open('GET', stringCall);
httpRequest.send();
}
Now Handler of Ajax calls in my . php:
if(@$_REQUEST['action'] == 'buscaConta')
{
buscaConta();
}
Everything’s fine with Handler at first. Now, the function " searchConta(); ":
function buscaConta(){
$con = ConectaDB();
$cpf = $con->real_escape_string($_REQUEST['cpf']);
$conta = $con->real_escape_string($_REQUEST['conta']);
$cpf = extraiSoNumsDeString($cpf);
$result = mysqli_query($con, "SELECT agencia,conta,saldo FROM contas WHERE dono='$cpf' AND conta='$conta' ORDER BY agencia");
$retData = Array();
$x=0;
while( $row = mysqli_fetch_array($result, MYSQLI_NUM)){
print_r($row);
$retData[] = $row[0];
}
$con->close();
echo json_encode($retData);
}
}
Extra functionSoNumsDeString($Cpf):
There’s nothing wrong with it, it returns a string of numbers.
Function organizationResultDaQuery($result); At first everything is fine with it
function organizaResultQuery($result){
$retData = Array();
while( $row = mysqli_fetch_array($result, MYSQLI_NUM)){
$retData[] = $row[0];
}
return $retData;
}
HERE APPEARS THE PROBLEM: AJAX CALL’s CALLBACK FUNCTION THAT RECEIVES THE RESULTING QUERY ARRAY FROM PHP. THE ARRAY ARRIVES HERE EMPTY...
On it, I simply print on the screen the query result.:
function formaCanvas(res){
console.log("Retorno da busca completa da conta: " +res);
}
The table searched in mysql:
I spent almost an hour debugging and I can’t fix it. Thanks already!
It’s not just doing
$retData[] = array('agencia' => $row[0], 'conta' => $row[1], 'saldo' => $row[2])
?– Marcelo Shiniti Uchimura
Worse than not. Because all Row content is in the first position of the array.
– Lucas Pletsch
I’ve already tried :(
– Lucas Pletsch
And what you literally have in the first position?
– Marcelo Shiniti Uchimura
An array containing the query output.
– Lucas Pletsch
You are not confusing
$row
with$result
, maybe?– Marcelo Shiniti Uchimura
Let’s go continue this discussion in chat.
– Marcelo Shiniti Uchimura
@Marcelouchimura. I managed to get this part of php right, but in function formaCanvas(res) I get just this: []
– Lucas Pletsch
Are you sure you are passing a number and an account to which you have given?
– Marcelo Shiniti Uchimura
Yes. If I echo the search functionConta, after that: $account = $con->real_escape_string($_REQUEST['account']); $Cpf = extraiSoNumsDeString($Cpf); Shows Cpf values and counts correctly.
– Lucas Pletsch
then the problem is still in the
organizaResultQuery()
– Marcelo Shiniti Uchimura
Yes. Now I see that it is. It is only working for the first account in the list of accounts of the client that I select when I see the balance
– Lucas Pletsch
This is another feature. It’s all wrapped up now. I’ll take a slow look
– Lucas Pletsch
WORSE THAN NOT. The line: $account = $con->real_escape_string($_REQUEST['account']); returns the account number I selected in the client account list correctly.
– Lucas Pletsch
Wait. I found a problem.
– Lucas Pletsch