不能从PHP调用javascript alert alertify库


Can't call javascript alert alertify library from PHP

我正在使用javascript alertify库来定制警报。

当我从javascript调用alertify一切都是好的,但是,如果我从PHP做它不工作。提示"Alertify未定义"。

是重要的说,如果我使用默认的javascript警报,它从PHP工作。

为什么会这样?

更新:

  // This works
  if(empty($user_password)){ 
     echo "<script>alert('Por favor, ingrese su usuario y contraseña');</script>";
  }

  // This doesnt works
  if(empty($user_password)){ 
     echo "<script>alertify.alert('Por favor, ingrese su usuario y contraseña');</script>";
  }
  // Alertify library is included

也许lib还没有准备好,试试:

if(empty($user_password)){ 
     echo "<script>$(function(){alertify.alert('Por favor, ingrese su usuario y contraseña');})</script>";
  }

这对我很有效。

使用onload事件在js|jquery触发alertify。
使用会话来存储反馈细节,并检查会话是否在回显之前设置。

<?php
//partial php code
if($saveData)
{
$_SESSION['variable'] = 'text here';
header('location:link.php');
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>Page Title</title>
    <link href="path/alertify.css"></link>
</head>
<body>
<?php if(isset($_SESSION["variable"])):?>
    
    <script>
        window.onload = () => {
            alertify.alert("Title", "<?=$_SESSION["variable"]?>");
        }
    </script>
    
    <?php unset($_SESSION["variable"]); endif;?>
    
    <script src="path/alertify.js"></script>
</body>