如何从PHP调用JavaScript函数?

<?php

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

下面的代码来自xyz.html(单击按钮),它调用外部xyz.js中的wait()。这个wait()调用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;
  }
}

和wait.php

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

其中loadxml()以同样的方式从另一个PHP文件调用代码。

除此之外,loadxml()工作得很好,但它没有以我想要的方式调用。


当前回答

我总是使用echo "<脚本>函数();> < /脚本”;或者类似的东西。从技术上讲,在PHP中没有调用函数,但这是最接近的方法。

其他回答

你也可以试试这个:-

    public function PHPFunction()
    {
            echo '<script type="text/javascript">
                 test();
            </script>'; 
    }
    <script type="text/javascript">
    public function test()
    {
        alert('In test Function');
    }
    </script>

就PHP而言(或者实际上是一般的web服务器),HTML页面只不过是一个大字符串。

你可以用像PHP这样的语言做的所有花哨的工作——从数据库和web服务中读取等等——最终的目标是完全相同的基本原则:生成一个HTML*字符串。

你的大HTML字符串不会变得比这更特别,直到它被web浏览器加载。一旦浏览器加载了页面,所有其他神奇的事情就发生了——布局、框模型、DOM生成和许多其他事情,包括JavaScript执行。

因此,您不是“从PHP调用JavaScript”,而是“在输出中包含JavaScript函数调用”。

有很多方法可以做到这一点,但这里有几个。

只使用PHP:

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

从php模式切换到直接输出模式:

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

你不需要返回函数名或类似的东西。首先,停止手工编写AJAX请求。你这是在为难自己。使用jQuery或其他优秀的框架。

其次,要明白,一旦从AJAX调用接收到响应,就已经在执行javascript代码了。

下面是一个示例,我认为您正在使用jQuery的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
}

现在,如果您执意要将函数名从PHP发送回AJAX调用,也可以这样做。

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

*或JSON或XML等。

这不可能。PHP是服务器端语言,JavaScript是客户端语言,它们彼此并不了解。您需要一个服务器端的JavaScript解释器(如Aptanas Jaxer)。也许您真正想做的是使用Ajax,如Architecture (JavaScript函数异步调用PHP脚本并对结果进行处理)。

<td onClick= loadxml()><i>Click for Details</i></td>

function loadxml()
{
    result = loadScriptWithAjax("/script.php?event=button_clicked");
    alert(result);
}

// script.php
<?php
    if($_GET['event'] == 'button_clicked')
        echo "\"You clicked a button\"";
?>

你不能。你可以从PHP输出的HTML中调用JS函数,但这完全是另一回事。

我不接受反对者的答案。

如果你发现了一些特殊的软件包可以让它工作,那么你可以自己做!所以,我不相信这些答案。

onClick是一个涉及最终用户的拼凑,因此是不可接受的。

@umesh很接近,但它不是一个独立的程序。以下是这样的(摘自他的回答):

<script type="text/javascript">
function JSFunction() {
    alert('In test Function');  // This demonstrates that the function was called
}
</script>

<?php
// Call a JS function "from" php

if (true) {   // This if() is to point out that you might
              // want to call JSFunction conditionally
    // An echo like this is how you implant the 'call' in a way
    // that it will be invoked in the client.
    echo '<script type="text/javascript">
         JSFunction();
    </script>';
}

函数在使用之前声明是很重要的。(我不知道“以前”是指“词汇上的以前”还是“时间上的以前”;在上面的示例代码中,两者都是。)