I have a php file func.php
where I defined many functions let's say :
<? php
function func1($data){
return $data+1;
}
?>
I want to call the function func1
using ajax. thank you for your help
Also, apart from doing this with the click of a button, a PHP function can be called using Ajax, JavaScript, and JQuery.
What you want is something like: $. ajax({ url: '/my/site', data: {action: 'test'}, type: 'post', success: function(output) { alert(output); } });
You can call PHP function through Javascript by passing the value, you need as output of PHP function as a string in Javascript.
AJAX allows web pages to be updated asynchronously by exchanging small amounts of data with the server behind the scenes. This means that it is possible to update parts of a web page, without reloading the whole page. Classic web pages, (which do not use AJAX) must reload the entire page if the content should change.
You can't call a PHP function directly from an AJAX call, but you can do this:
PHP:
<? php
function func1($data){
return $data+1;
}
if (isset($_POST['callFunc1'])) {
echo func1($_POST['callFunc1']);
}
?>
JS:
$.ajax({
url: 'myFunctions.php',
type: 'post',
data: { "callFunc1": "1"},
success: function(response) { alert(response); }
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With