I have this code:
JavaScript:
function foo(callback){
var producto = $("#inpt-producto").val();
var ajax = new XMLHttpRequest();
var url = "file_products.php"; // It will return 1 or 0
var params = "producto=" + producto;
ajax.open("POST", url, true);
ajax.onreadystatechange = function()
{
if(ajax.readyState == 4 && ajax.status == 200) {
callback(ajax.responseText);
}
};
ajax.send(params);
}
PHP:
$objectProduct = new product_Model();
$option = $objectProduct->existProduct($codProduct); // return 1 or 0
echo $option
It Works fine for me. But the next code didn't work.
var encontrado = foo(function(result){ // I need store it in "encontrado" variable.
console.log(result);//Actually return 1 or 0 value
});
console.log(encontrado); //Return Undefined :-(
The file file_products.php
return 1 or 0.
The variable encontrado
didn't store data. It is equal to "Undefined". I need store PHP value return. I have been working hard but didn't found the solution.
What can I do to fix it ? Any idea ?
Thanks.