Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a JavaScript function from PHP?

Tags:

javascript

php

How to call a JavaScript function from PHP?

<?php

  jsfunction();
  // or
  echo(jsfunction());
  // or
  // Anything else?

The following code is from xyz.html (on a button click) it calls a wait() in an external xyz.js. This wait() calls wait.php.

function wait() 
{
  xmlhttp=GetXmlHttpObject();
  var url="wait.php"; \
  xmlhttp.onreadystatechange=statechanged; 
  xmlhttp.open("GET", url, true); 
  xmlhttp.send(null);
} 

function statechanged()
{ 
  if(xmlhttp.readyState==4) {
       document.getElementById("txt").innerHTML=xmlhttp.responseText;
  }
}

and wait.php

<?php echo "<script> loadxml(); </script>"; 

where loadxml() calls code from another PHP file the same way.

The loadxml() is working fine otherwise, but it is not being called the way I want it.

like image 577
Zeeshan Rang Avatar asked Oct 21 '22 21:10

Zeeshan Rang


People also ask

Can you call JavaScript function in PHP?

You can execute Javascript through PHP by calling javascript code/function as a string in PHP and send it to the client browser to execute.

Can PHP and JavaScript work together?

JavaScript can also talk with your PHP code on the web server whenever it needs to update something (either on the server or on the web page).

How do I call a function in JavaScript?

Invoking a function with function constructor in JavaScript In constructor invocation, a function is invoked utilizing the “new” keyword. By utilizing the “new” keyword, you can generate a new object that inherits the properties of the created constructor function.


1 Answers

As far as PHP is concerned (or really, a web server in general), an HTML page is nothing more complicated than a big string.

All the fancy work you can do with language like PHP - reading from databases and web services and all that - the ultimate end goal is the exact same basic principle: generate a string of HTML*.

Your big HTML string doesn't become anything more special than that until it's loaded by a web browser. Once a browser loads the page, then all the other magic happens - layout, box model stuff, DOM generation, and many other things, including JavaScript execution.

So, you don't "call JavaScript from PHP", you "include a JavaScript function call in your output".

There are many ways to do this, but here are a couple.

Using just PHP:

echo '<script type="text/javascript">',
     'jsfunction();',
     '</script>'
;

Escaping from php mode to direct output mode:

<?php
    // some php stuff
?>
<script type="text/javascript">
    jsFunction();
</script>

You don't need to return a function name or anything like that. First of all, stop writing AJAX requests by hand. You're only making it hard on yourself. Get jQuery or one of the other excellent frameworks out there.

Secondly, understand that you already are going to be executing javascript code once the response is received from the AJAX call.

Here's an example of what I think you're doing with jQuery's AJAX

$.get(
    'wait.php',
    {},
    function(returnedData) {
        document.getElementById("txt").innerHTML = returnedData;

        //  Ok, here's where you can call another function
        someOtherFunctionYouWantToCall();

        // But unless you really need to, you don't have to
        // We're already in the middle of a function execution
        // right here, so you might as well put your code here
    },
    'text'
);

function someOtherFunctionYouWantToCall() {
    // stuff
}

Now, if you're dead-set on sending a function name from PHP back to the AJAX call, you can do that too.

$.get(
    'wait.php',
    {},
    function(returnedData) {
        // Assumes returnedData has a javascript function name
        window[returnedData]();
    },
    'text'
);

* Or JSON or XML etc.

like image 411
Peter Bailey Avatar answered Oct 24 '22 09:10

Peter Bailey