简体   繁体   English

从PHP调用Javascript函数

[英]Call to Javascript function from php

I am having difficulties when calling Javascript functions from inside php. 从php内部调用Javascript函数时遇到困难。 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: 在PHP内部,函数声明为:

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调用JavaScript函数。 PHP is a server side language while JavaScript is a client side language. PHP是服务器端语言,而JavaScript是客户端语言。 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: 根据您的要求和功能意图,您可以将myFunction重写为PHP函数,然后调用它:

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

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

You can get php to output a self calling function like this 您可以使php输出这样的自调用函数

<?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. 那应该在浏览器点击时执行js,您可以在其中使用php变量,因为它们会被服务器扩展。 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 或者,如果您确实需要服务器端php,则可以对该php脚本进行ajax调用并获取一些json

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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