简体   繁体   English

从 php 呼叫 javascript function 无效

[英]Calling javascript function from php not working

My research into this was rather frustrating, though I don't see why this doesn't work.我对此的研究相当令人沮丧,尽管我不明白为什么这不起作用。

When my index page loads its supposed to load data from a database via php and then echo the data to javascript where javascript formats and works with that data, this works fine if I place an onload event on the body of the html body tag which will then utilize the data loaded into the js variable data.当我的索引页面加载它应该通过 php 从数据库加载数据,然后将数据回显到 javascript,其中 javascript 格式并使用该数据时,如果我在 html 主体标签的主体上放置一个 onload 事件,这将正常工作然后利用加载到js变量数据中的数据。 This works but I don't like using the onload event every time the page loads.这可行,但我不喜欢每次加载页面时都使用 onload 事件。

Here is the php code which is at the top of the index.php file:这是位于 index.php 文件顶部的 php 代码:

if ($confirmation){

    $data = $membership->get_data("assump");

    echo '<script type="text/javascript">var data = '. json_encode($data) .';
        var dataLoaded = ' . json_encode(false) . '; loadData();</script>';

}

The loadData() function: loadData() function:

<script type="text/javascript">

    function loadData(){

        // Do stuff with the data

    }
</script>

The js function loadData() is written further down in the same php file before the closing tag body. js function loadData() 在结束标记正文之前的同一个 php 文件中进一步写入。 Should I have php build the script instead?我应该让 php 来构建脚本吗? or place the script somewhere else?或者将脚本放在其他地方? Or is it possible to call this js function?或者可以调用这个 js function 吗?

Seems like you're having two pieces of code in two separate script blocks. 似乎您在两个单独的脚本块中有两个代码段。 That won't work. 那行不通。

In order for function hoisting to work, they need to be in the same tag (the hoisted function needs to be defined there). 为了使功能提升工作,它们必须位于相同的标签中(需要在此处定义提升的功能)。

Here is a similar question with a more detailed answer: Why is my javascript function not being called in my div? 这是一个带有更详细答案的类似问题: 为什么我的div中没有​​调用我的javascript函数?


UPDATE (per request) 更新(每个请求)

The browser reads one block of JS at a time, so hoisting works only within the same block (or if the function is defined before, of course). 浏览器一次读取一个JS块,因此提升仅在同一块内进行(或者,当然,如果函数是在之前定义的)。 In your case, probably the easiest solution would be to assign the code to a PHP variable, and then just echo it in the same script block: 在您的情况下,可能最简单的解决方案是将代码分配给PHP变量,然后在同一脚本块中将其回显:

PHP 的PHP

$js = '';

if ($confirmation){

    $data = $membership->get_data("assump");

    $js .= 'var data = '. json_encode($data) .';
        var dataLoaded = ' . json_encode(false) . '; loadData();';

}

HTML 的HTML

<script type="text/javascript">

    <?= $js ?> // or <?php echo $js; ?>

    function loadData(){

        // Do stuff with the data

    }
</script>

Note that this allows you to inject several lines/parts of JS via PHP, that's why I used $js .= ... instead of just $js = ... . 请注意,这允许您通过PHP注入JS的几行/部分,这就是为什么我使用$js .= ...而不是$js = ...

Note that this won't work with external .js files unless you make PHP parse them as well first (nothing to worry about in your code since your JS is in the HTML file which is parsed normally). 请注意,除非您先使PHP也解析它们,否则它将不适用于外部.js文件(因为您的JS位于正常解析的HTML文件中,因此无需担心代码中的代码)。

echo "<script>function fBefore(x) { console.log('before'); }</script>\n";
echo "<script>fBefore(); fAfter(); </script>\n";
echo "<script>function fAfter(x)  { console.log('after');  }</script>\n";

console log: (Chrome)控制台日志:(Chrome)

before
Uncaught ReferenceError: fAfter is not defined

I conclude that the function definition must be before the use, and may be in a separate script block.我的结论是 function 定义必须在使用之前,并且可能在单独的脚本块中。

That was from PHP creating blocks of text.那是来自 PHP 创建的文本块。 I did not test onLoad , or <script src=...> , or other ways to shuffle things around.我没有测试onLoad<script src=...>或其他随机播放的方法。 I worry that "must be declared before calling" may refer to the physical placement of code, or dynamic loading, or something else.我担心“必须在调用前声明”可能指的是代码的物理放置,或者动态加载,或者其他什么。

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

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