我的代码-
<?php
session_start();
$_SESSION['errors']="failed";
?>
<head>
function myfunc()
{
alert(<?php echo $_SESSION['errors']; ?>);
}
</head>
<body onload="myfunc();">
但未弹出警报消息。
最佳答案
代码中的两个错误:
- 你失踪了
<script>
标签 - 您在警报中缺少单引号/双引号
试试这个:
<?php
session_start();
$_SESSION['errors']="failed";
?>
<head>
<script>
function myfunc()
{
alert('<?php echo $_SESSION["errors"]; ?>');
}
</script>
</head>
你可能想把 myfunc()
在window.load
事件或一些click
事件来测试它。此外,正如 ThiefMaster 的正确建议,您可能需要使用 addslashes
$_SESSION["errors"]
的函数在alert
.
注意:假定您的代码的文件扩展名为 php。
关于php - 如何在javascript中插入PHP代码片段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2863675/