我有一个 php 脚本,我试图用 Javascript 触发它。我正在尝试使用变量通过 php 动态设置 URL,但没有任何运气,有人知道我应该如何处理这个问题吗?
<a href="#" onclick="return getOutput();">Click here</a>
<?php $update_url = 'https://' . $_SERVER['SERVER_NAME'] . '/xyz.php'; ?>
function getOutput() {
getRequest(
var str = "<?php echo $update_url ?>", // URL for the PHP file
drawOutput, // handle successful request
drawError // handle error
);
return false;
}
最佳答案
这是你可以做的;将 PHP 值作为字符串传递。
<?php $update_url = 'https://' . $_SERVER['SERVER_NAME'] . '/xyz.php'; ?>
function getOutput() {
getRequest(
"<?php echo $update_url ?>", // URL for the PHP file
drawOutput, // handle successful request
drawError // handle error
);
return false;
}
或
在全局范围内定义为 JavaScript 值(这可能不起作用)
var updateURL = "<?php $update_url = 'https://' . $_SERVER['SERVER_NAME'] . '/xyz.php'; ?>";
function getOutput() {
getRequest(
updateURL, // URL for the PHP file
drawOutput, // handle successful request
drawError // handle error
);
return false;
}
关于javascript - 使 PHP 生成的 URL 可在 Javascript 函数中触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38197907/