'Can't call javascript alert alertify library from PHP
I am using javascript alertify library for customized alerts.
When I call alertify from javascript everything is ok, but, If I do It from PHP doesnt works. THe error say "Alertify is not defined".
Is important to say that If I use default javascript alerts It works from PHP.
Why is happing this?
Regards
UPDATE:
// 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
Solution 1:[1]
Maybe the lib isn't ready yet, try:
if(empty($user_password)){
echo "<script>$(function(){alertify.alert('Por favor, ingrese su usuario y contraseƱa');})</script>";
}
Solution 2:[2]
This worked for me.
Use onload event on js|jquery to trigger alertify.
Use sessions to store feedback detail and check if sessions are set before echoed.
<?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>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Toni Michel Caubet |
Solution 2 |