[英]Passing php variables through the parameter to a javascript trigger function, PHP, JAVASCRIPT
[英]Passing php variables through the parameters to a javascript function, PHP, JAVASCRIPT
我正在嘗試通過名為alertText的php函數的參數將字符串變量傳遞給名為alert的javascript函數,以將消息提示給屏幕,但是由於某種原因它不接受php變量,因此沒有任何提示屏幕。 請看一下:
testing.php
function alertText($text) {
echo "
<script>
alert($text);
</script>
";
}
alertText("Hello");
?>
您需要單引號$ text變量,因為JavaScript無法將該變量識別為字符串。
<?php
function alertText($text) {
echo "<script>alert('$text');</script>";
}
alertText("Hello");
您可以將其連接起來以獲得更易讀的代碼。
function alertText($text) {
echo "<script>alert('".$text."');</script>";
}
alertText('Hello');
很簡單的錯誤,您忘了關閉雙引號,讓解析器知道$ text不是單詞$ text(嘿,它可能發生!)。
我們使用。$ variable_name。 在運行時將變量的值與javascript函數連接在一起。
<?php
function alertText($text) {
echo "
<script>
alert('".$text."');
</script>
";
}
alertText("Hello");
?>
**示例更正**
你應該像這樣使用它
function alertText($text) {
echo '<script type="text/javascript">alert("' . $text . '"); </script>';
";
}
alertText("Hello");
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.