I have problems with AJAX results using jQuery.
I have defined these functions:
<script>
function hello(callback, funct, val) {
var ret = 0;
console.log(val);
$.ajax({
type: 'GET',
dataType: 'json',
url: 'SGWEB/header.php',
data: {
'funct': funct,
'val': val
}
}).done(function (data) {
// you may safely use results here
console.log(data);
callback(data);
});
};
function change() {
hello(function (ret) {
console.log(ret);
$("#b1").text(ret);
}, "hello", 1);
};
change();
</script>
SGWEB/header.php:
extract($_GET);
$validFunctions = array("readPin","hello");
if(in_array($funct, $validFunctions)) $funct();
// functions
// ....
function hello($val) {
if ($val == 1) {
echo "1";
} else
echo "2";
}
The problem I have is that the AJAX passes only the first parameter in data {'funct': funct} and it's working, but val is completely ignored (it always echoes "2").
How can I solve this? Thanks
hello()which is false when you compare it to1.. that's why it always goes to theelsebranch.SGWEB/header.phpto see how it's handling the incoming query to end up calling the hello function ... surely the code marked PHP function isn't the whole content of that header.phpSGWEB/header.phpand I dont see how you are obtaining your GET data in PHP.