javascript - 如何从不同的 php 文件调用 javascript 函数

标签 javascript php html hyperlink

我想从 php 文件 (xyz.php) 调用一个 javascript 函数(包含在 html 文件中:index.html)。 因此,当我单击 xyz.php 页面内的链接时,它将调用 index.html 中的 javascript 函数。

类似于:

echo '<a href="#" onclick="index.html.someFunction(e)"></a>';

它需要以某种方式访问​​函数所在的 index.html。

编辑:

Index.html

<html>
 <head>    
 </head>
 <body>
<button id="search_button" type="submit" value= "Search" onclick= "return getOutput()">Search</button>

<div id="output" style="width:395px; height:150px; overflow: auto; background = #969696" >    </div>
 </body>

 <script>    


    //THIS DISPLAYS THE CONTENT OF MY PHP PAGE INSIDE THE DIV FIELD
    function getOutput() {
      getRequest(
          "xyz.php?eingabe=123&eingabe2=File.csv", // URL for the PHP file
           drawOutput,  // handle successful request
           drawError    // handle error
      );
      return false;
    }
    function getOutput(link) {
      getRequest(
          link, // URL for the PHP file
           drawOutput,  // handle successful request
           drawError    // handle error
      );
      return false;
    }

    // handles drawing an error message
    function drawError() {
        var container = document.getElementById('wagoartikelnr2');
        container.innerHTML = 'Bummer: there was an error!';
}
    // handles the response, adds the html
    function drawOutput(responseText) {
        var container = document.getElementById('wagoartikelnr2');
        container.innerHTML = responseText;
            }
    // helper function for cross-browser request object
    function getRequest(url, success, error) {
        var req = false;
        try{
            // most browsers
            req = new XMLHttpRequest();
        } catch (e){
            // IE
            try{
                req = new ActiveXObject("Msxml2.XMLHTTP");
            } catch(e) {
                // try an older version
                try{
                    req = new ActiveXObject("Microsoft.XMLHTTP");
                } catch(e) {
                    return false;
                }
            }
        }
        if (!req) return false;
        if (typeof success != 'function') success = function () {};
        if (typeof error!= 'function') error = function () {};
        req.onreadystatechange = function(){
            if(req.readyState == 4) {
                return req.status === 200 ? 
                    success(req.responseText) : error(req.status);
            }
        }
        req.open("GET", url, true);
        req.send(null);
        return req;
    }


 </script>
 </html>

XYZ.PHP

<html>
<?php

$vergleich = $_GET["eingabe"];
$datei = $_GET["eingabe2"]; 




 $temp2a = array("0.1.2.4", "0.1.2.3", "0.1.2.2");


for ($i = 0; $i < count($temp2a); $i++) {

                $URL = "xyz.php?eingabe=',temp2a[$i],'&eingabe2=',$datei,";
                echo '<a href=',temp2a[$i],' onClick= "index.html.getOutput($URL)"></a>';
            } 
?> 

<body>
</body>
</html>

最佳答案

您可以将 Javascript 代码保存在外部 js 文件中,并通过 script 将其包含在您的 html/php 文件中。标签;

<script src="path/to/your/js/file.js"></script>

编辑: 好的,首先,您需要完全删除重载的 getOutput(link) 函数。这将使您对 xyz.php 文件的调用指向实际的 php 文件,否则它指向一个名为 undefined 的文件(因为您没有提供链接参数)。然后,您的 xyz.php 文件中的第 16,17 行有一个拼写错误:

$URL = "xyz.php?eingabe=',temp2a[$i],'&eingabe2=',$datei,";
echo '<a href=',temp2a[$i],' onClick= "index.html.getOutput($URL)"></a>';

应该改成这样;

// Note the missing $ sign from your temp2a variables and the missing quotes from the anchor tag
$URL = "xyz.php?eingabe=',$temp2a[$i],'&eingabe2=',$datei,";
echo '<a href="',$temp2a[$i],'" onClick= "getOutput($URL)"></a>';</a>';

EDIT2:在您的文件中发现了其他一些小错误;

index.html:

<html>
 <head>
 </head>
 <body>
<button id="search_button" type="submit" value= "Search" onclick= "return getOutput()">Search</button>

<div id="wagoartikelnr2" style="width:395px; height:150px; overflow: auto; background = #969696" >    </div>

<script src="script.js"></script>
 </body>
 </html>

xyz.php:

<?php

$vergleich = $_GET["eingabe"];
$datei = $_GET["eingabe2"];
$temp2a = array("0.1.2.4", "0.1.2.3", "0.1.2.2");

for ($i = 0; $i < count($temp2a); $i++) {
    $URL = "xyz.php?eingabe=".$temp2a[$i]."&eingabe2=".$datei;
    echo '<p><a href="'.$temp2a[$i].'" onClick="getOutput("'.$URL.'")">link</a></p>';
}

?>

脚本.js:

    function getOutput(url) {
      url = url || "xyz.php?eingabe=123&eingabe2=File.csv";
      getRequest(
          url, // URL for the PHP file
           drawOutput,  // handle successful request
           drawError    // handle error
      );
      return false;
    }

    // handles drawing an error message
    function drawError() {
        var container = document.getElementById('wagoartikelnr2');
        container.innerHTML = 'Bummer: there was an error!';
}
    // handles the response, adds the html
    function drawOutput(responseText) {
        var container = document.getElementById('wagoartikelnr2');
        container.innerHTML = responseText;
            }
    // helper function for cross-browser request object
    function getRequest(url, success, error) {
        var req = false;
        try{
            // most browsers
            req = new XMLHttpRequest();
        } catch (e){
            // IE
            try{
                req = new ActiveXObject("Msxml2.XMLHTTP");
            } catch(e) {
                // try an older version
                try{
                    req = new ActiveXObject("Microsoft.XMLHTTP");
                } catch(e) {
                    return false;
                }
            }
        }
        if (!req) return false;
        if (typeof success != 'function') success = function () {};
        if (typeof error!= 'function') error = function () {};
        req.onreadystatechange = function(){
            if(req.readyState == 4) {
                return req.status === 200 ?
                    success(req.responseText) : error(req.status);
            }
        }
        req.open("GET", url, true);
        req.send(null);
        return req;
    }

关于javascript - 如何从不同的 php 文件调用 javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28764628/

相关文章:

javascript - Angular 2 发送空帖子

php - 如何在 PHP 中获得 C 的连续输出

c# - 如何格式化 mssql 日期时间列以仅在网页输入字段上显示时间?

javascript - Backbone js 路由器 - 组织 View 的更好方法

当涉及转义字符时,Javascript eval 无法评估 json

php - 保存与 Yii 有多对多关系的模型

jquery - 隐藏内容扭曲了 css 多列布局上的列

javascript - 哪些视频播放器不需要文件格式转换?

javascript - 创建后更新 Apollo 客户端身份验证链接

javascript - 如何删除 Krajee Bootstrap 文件输入中的图像?