我想从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>
答案 0 :(得分:3)
您可以将Javascript代码保存在外部js文件中,并通过script
标记将其包含在html / php文件中;
<script src="path/to/your/js/file.js"></script>
编辑:好的,首先,您需要完全删除重载的getOutput(链接)功能。这将使您对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>';
}
?>
的script.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;
}
答案 1 :(得分:0)
使用此代码
<script type="text/javascript" src="path_to_ur_filename.js"></script>