在 JS 警报中显示 PHP 变量

Show PHP variable in JS alert

我正在尝试提醒消息,但它不适用于我的字符串。

 echo "<script>alert('$lang['ALERT']');
 window.location.href ='index.php';
 </script>";

不工作...帮助

你在混淆引号。保持简单,

echo "<script>alert('".$lang['ALERT']."');
      window.location.href ='index.php';
      </script>";

试一试。

<script>
var alertMsg = "<?php echo $lang['ALERT'] ?>";
alert(alertMsg);
window.location.href ='index.php';
</script>;

您没有正确引用您的变量

<?php
$lang['ALERT'] = 'mystring';
echo "<script>alert('".$lang['ALERT']."');window.location.href ='index.php';</script>";
?>

Here's the eval

试试这个

<script>
    alert(<?=$lang['ALERT']; ?>);
    window.location.href ='index.php';
</script>

引用是问题

 <?php $alert=$lang['ALERT'];?>

   <script>
    alert('<?php echo $alert?>');
    window.location.href ='index.php';
   </script>;