I have an application in which, a Javascript I have a function that performs an Ajax call to the .php file to handle. The .php handles the SearchContact (p) function like this: it does a SELECT on the MySql database (managed via XAMPP and phpMyAdmin), takes the result of that query, saves it to an array, and returns it to another function Javascript (callBack), called "formCanvas (p)" in Json format. But what I get in my JS callback 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 does I get the correct result on it: the agency number, account number, and balance. . What would be a correct empty array?
an array filled with values ...
Why does it get empty for the formCanvas () function of JS? Where am I going 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 the Ajax call handler in my .php:
if(@$_REQUEST['action'] == 'buscaConta')
{
buscaConta();
}
At first, all right with the handler. Now the function "searchContact ();":
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);
}
}
Function extraiSoNumsDeString ($ cpf):
There's nothing wrong with it, it returns a String of numbers.
Organizational functionResultDaQuery ($ result); At first all right with it
function organizaResultQuery($result){
$retData = Array();
while( $row = mysqli_fetch_array($result, MYSQLI_NUM)){
$retData[] = $row[0];
}
return $retData;
}
HERE THE PROBLEM APPEARS: THE CALLBACK FUNCTION OF AJAX CALL THAT RECEIVES FROM PHP THE ARRAY THAT RESULTS FROM QUERY. ARRAY COMES HERE EMPTY ...
In it, I simply print the result of the query on the screen:
function formaCanvas(res){
console.log("Retorno da busca completa da conta: " +res);
}
The table searched in mySQL:
I have spent almost an hour debugging and can not fix. Thank you in advance!