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

查看:47
本文介绍了如何从 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>"; 

where 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天全站免登陆