[英]Make PHP generated URL trigerable in Javascript function
我有一個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;
}
您不能將var str =
作為function()
的參數。 你不會寫
functionName(var a='a', b); // this will not work
這是解決該問題的一種方法:
function getOutput() {
var str = "<?php echo $update_url ?>"; // URL for the PHP file
getRequest(
str,
drawOutput, // handle successful request
drawError // handle error
);
return false;
}
而且這種方式更短:
function getOutput() {
getRequest(
<?php echo $update_url ?>, // URL for the PHP file
drawOutput, // handle successful request
drawError // handle error
);
return false;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.