简体   繁体   中英

Call to Javascript function from php

I am having difficulties when calling Javascript functions from inside php. First of all, I assume that is possible?! Secondly, how do you do it? Currently I have:

echo myFunction($name1);

inside the php and the function is declared as:

function myFunction(name)

but this gives:

Fatal error: Call to undefined function myFunction() in C:\xampp\htdocs\page.php on line 109

Please Help!

It is not possible to call a JavaScript function from PHP. PHP is a server side language while JavaScript is a client side language. The two know nothing about one another.

EDIT:

Depending on your requirements and the functions intent, you may be able to rewrite myFunction as a PHP function and then call it:

/* Create the function */
function myFunction($name) {
    ...
}

/* Call the function */
myFunction("name");

You can get php to output a self calling function like this

<?php
print "<script>(function (){
  //JavaScript stuff
})();”; ?>

That should execute the js when the browser hits it, you can use php variables inside this as they will be expanded by the server. Your probably better off writing the functionality in a different way tho. Or if you really need server side php involved, to make an ajax call to that php script and grab some json

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM