Call JavaScript Function From A Php Script
However, server side does not have a direct link to your client context. So the are two ways to run this method as a result of the server processing.
1) to send a ajax request to the server, and, based on the result, execute the method.
2) If you don't want to deal with ajax cal logic, you may do a script insertion approach. How it works, is that when the button is pressed you do something like this:
var scriptRef = document.createElement('script');
scriptRef.setAttribute('src','url_to_script.php');
document.head.appendChild(scriptRef);
What it will do, it will dynamically append a script tag to your document and load script content as a result of your php script output. There you can decide to launch the method or do something else.
However, script insertion is a bit of a headache and should not be used on a long-running pages (in my opinion), as it actually adds a new script every time you press a button. And its your responsibility to keep track of it and remove it.
Solution 2:
As long as you display your functions correctly in JavaScript format You can just call:
<script type="text/javascript" src="ajax.php"></script>
Post a Comment for "Call JavaScript Function From A Php Script"