如何从PHP调用JavaScript函数? [英] How to call a JavaScript function from PHP?

查看:115
本文介绍了如何从PHP调用JavaScript函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从PHP调用JavaScript函数?

How to call a JavaScript function from PHP?

<?php

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

以下代码来自 xyz.html (按一下按钮)它在外部 xyz.js 中调用 wait()。这个 wait()调用wait.php。

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;
  }
}

wait.php

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

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

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

否则 loadxml()工作正常,但它没有按照我想要的方式调用。

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

推荐答案

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

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

您可以使用PHP等语言进行所有精彩的工作 - 从数据库和Web服务中读取所有这些 - 最终目标是完全相同的基本原则:生成一个HTML *字符串。

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*.

在Web浏览器加载之前,您的大HTML字符串不会变得比这更特别。一旦浏览器加载了页面,然后就会发生所有其他魔术 - 布局,盒子模型,DOM生成以及许多其他事情,包括JavaScript执行。

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.

所以,你不从PHP调用JavaScript,你在输出中包含一个JavaScript函数调用。

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.

仅使用PHP:

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

从php模式转到直接输出模式:

Escaping from php mode to direct output mode:

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

您无需返回函数名称或类似名称。首先,停止手动编写AJAX请求。你只会让自己变得困难。获取jQuery或其他优秀框架之一。

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.

其次,了解一旦从AJAX调用收到响应,您已经将执行javascript代码。

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

以下是我认为你在使用jQuery的AJAX做的一个例子

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
}

现在,如果您在将函数名称从PHP发送回AJAX调用时已经死定,那么您也可以这样做。

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'
);

*或JSON或XML等。

* Or JSON or XML etc.

这篇关于如何从PHP调用JavaScript函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆