-2

I want to invoke a javascript function from php, but php script is included before the javascript.

<script type="text/javascript">
function myjsfunction(){
//js code
}
</script>
Rajan
  • 105
  • 1
  • 8

4 Answers4

2

Slightly odd question...

If you want to call a JS function on page load you could add that to the bottom of the DOM on page load from the page created by your PHP script... For instance...

<script>
window.onload = function() {
  yourFunction(param1, param2);
};
</script>
Matt The Ninja
  • 2,641
  • 4
  • 28
  • 58
2

To communicate, with a back-end from Javascript we traditionally use AJAX. ( More on that subject is here, http://www.w3schools.com/php/php_ajax_php.asp ).

But since it uses long-term polling ( thus you have javascript request a certain PHP call every say, 500 milliseconds ) it generates a lot of load when dealing with loads of visitors, next to the fact it isn't exactly realtime, you might want to consider using websockets, which allows for bidirectional communication.
http://en.wikipedia.org/wiki/WebSocket

vInsert
  • 94
  • 3
2

This is not a bad question, however it could be possible that it's based on a bit of misunderstanding when it comes to the basic and fundamental principles of how client-server and generally web-based applications work.

Here's an example that I will use in order to illustrate your question: You are programming a web-based water kettle controller, and you'd like to display a notification on the web page of the controller when has the kettle reached boiling temperature, so the user can be notified by a message that tea can be served.

The situation in general looks like this:

  1. PHP code is executed at server side (e.g. the web server executes it and passes only the execution output to the client)
  2. JavaScript code is executed at client side (by the web browser)
  3. The web server and web browser communication is typically done using the HTTP protocol, using a set of requests (sent by the client) and answers (sent by the server).

The client sends a request, the server answers. What you'd like to do is that the server sends a request to the client to execute a script. This could make sense in the context of some event-drive or asynchronous JS frameworks. I'll entertain that for a bit, even though I do not understand what exactly you are trying to achieve.

You have the following options:

The stereotypical "synchronous" programming answer (for cases other than the kettle) - if your execution condition is known at the time the PHP script is interpreted, then generate/include the JS dynamically in the response/output page, just as Matt suggested. Example:


if (sessionActive()) {
$pageScript .= 'function runOnLoad() { alert("hit"); }
               window.onload = runOnLoad; '
    }
// ... do the rest
echo $pageScript;

Asynchronous execution, or event-oriented execution is when you do not know the condition at the time of page rendering (main PHP script execution) - this is the more interesting or non-trivial part of the question.

Option A: Continuously poll the server using AJAX calls. This is lame, slow and terribly inefficient.

Option B: Poll the server using an AJAX call, and the server takes time to respond to the request (PHP script loops polling or waits until the kettle is ready, and then sends the response). The huge disadvantage is that both servers and clients have timeouts for script execution. This would only work within a limited set of cases.

Option C: Implement connection-oriented protocol, which maintains a continuous connection with the server - e.g. WebSockets, which allows for an event handler to be called every time you receive information from the server. This requires a bit more technical prowess, however it's potentially the most efficient and scalable solution.

Milen
  • 1,510
  • 1
  • 10
  • 8
1

PHP is a server side scripting language. JS is (in your case) a client side scripting language. JS is executed "after" client page loading. So you can include two scripts with PHP and at the end of page loading call them (onload function).

Rémy J
  • 108
  • 5