PHP is the most powerful and popular server-side scripting language used worldwide whereas JavaScript is the only client-side web-based scripting language used with most of the popular websites and is an important scripting language which is supporting vital and dynamic functions on the various website that includes Google, Facebook and even WordPress powered websites.
It is also used with services and features offered by popular websites that include live chat, dynamic content load without refreshing the page etc.
In this article, we’ll learn to schedule to call PHP function in the interval of seconds or minutes using JavaScript.
1. Creating a PHP function to be called
You can create a separate PHP file with the function that you want to call using Javascript or you can write the PHP function within the same file from which you want to call and fire the function on passing some query-string.
Calling PHP function from a separate file:
function testfunction(){ echo 'Hi! this is test'; }
Calling PHP function from the same file:
if(isset($_GET["call_fun"])){ testfunction(); } function testfunction(){ echo 'Hi! this is test'; }
2. Calling or getting contents of the file
You can use jQuery to call the function or get the content of the file.
To call the function or to get its contents from the separate file:
$.get('http://example.com/file.php', function(data) { alert(data); });
To call the function or to get its contents from the same file:
$.get('http://example.com/file.php?call_fun', function(data) { alert(data); });