Reputation: 167
How can I get the returned value?
On click my button invokes:
$("#generateNumber").click(function(){
$.post( "./result.php", function( data ) {
console.log( data );
$(".generate_number").html(data);
});
});
my result.php:
<?php
$a = "returnedValue";
?>
but my console log doesn't show me my returnedValue. Why not?
Upvotes: 1
Views: 119
Reputation: 20486
You need to dump the value in result.php
:
<?php
$a = "returnedValue";
echo $a;
?>
The data parameter in the callback you use in jQuery.post()
contains the output of result.php
. Your PHP script wasn't outputting anything, though. Make sure you dump any data you want the AJAX call to read.
If you want to call a specific function in your PHP script, you'll probably need to pass some parameters to jQuery.post()
..a simple example:
$("#generateNumber").click(function(){
$.post("./result.php", {
functionName: 'foo'
}, function( data ) {
console.log( data );
// foo
});
});
And in result.php
:
<?php
function foo() { return "foo"; }
function bar() { return "bar"; }
switch($_POST['functionName']) {
case 'foo': echo foo(); break;
case 'bar': echo bar(); break;
}
?>
Upvotes: 3