问题
I am trying to echo a alert/notification box. This one is working fine -
echo '<script language="javascript">';
echo 'alert("Record successfully added in database")';
echo '</script>';
But I want to give timing for disappear the box after 2 seconds. I have no idea how to do it. I found through sweetalert it is possible. So I did like this -
echo '<script src="sweetalert/lib/sweet-alert.min.js">';
echo '<link rel="stylesheet" type="text/css" href="sweetalert/lib/sweet-alert.css">';
echo 'swal({ title: "Record added successfully!",
text: "I will close in 2 seconds.",
timer: 2000
});';
echo '</script>';
Any help will be greatly appreciated.
回答1:
I also faced same problem, when I used:
echo '<script> swal("....");</script>';
only...
After one and half day of try, I overcome to this issue, as mention below:
<?php
echo '<script type="text/javascript">';
echo 'setTimeout(function () { swal("WOW!","Message!","success");';
echo '}, 1000);</script>';
?>
HOPE it will help you and others too...
回答2:
I had no problem typing the library to the top of the file like this :
<html>
<head><title></title>
<script src="sweet/sweetalert.min.js"></script>
<link rel="stylesheet" type="text/css" href="sweet/sweetalert.css">
<link rel="stylesheet" type="text/css" href="sweet/facebook.css">
</head>
<body>
then i opened the php code
<?php include ('conexion.php'); .....
echo "<script language = javascript>
swal({ title: 'Información',
text: 'Se ha enviado su petición!',
type: 'success',
showCancelButton: false,
closeOnConfirm: false,
confirmButtonText: 'Aceptar',
showLoaderOnConfirm: true, },
function(){
setTimeout(function(){
location = 'Frm.php';
});
});
</script>";
and all is working well. Regards!
回答3:
Just call the custom javascript function that includes the sweet alert from php echo as given below:
<header>
<link rel="stylesheet" type="text/css" href="....'path to ur sweet alert.css'">
<script type="text/javascript" src="....'path to ur sweetalert.js'"></script>
<script type="text/javascript">
function myFn(){
swal({
title: "Success",
text: "Thank you for contacting us. We will get back to you soon!",
type: "success"
},
function(){
//event to perform on click of ok button of sweetalert
});
}
</script>
</header>
<?php
echo '<script type="text/javascript">
myFn();
</script>';
?>
//Hope this will work for you.. as it worked for me
回答4:
<script>
var swaltitle,swaltext,swallink,swaltype,swallocation,confirmButtonText;
function showSwal()
{
swal({
title: swaltitle,
text: swaltext,
type:swaltype,
showCancelButton: false,
confirmButtonText:confirmButtonText,
timer: 3000
}).then(
function ()
{
window.location.href=swallocation;
},
function (dismiss)
{
if (dismiss === 'timer')
{
window.location.href=swallocation;
}
}
)
}
</script>
</head>
<body onload="showSwal()">
And then you can set the value of all swal varibles by php echo and also do not forget to include CSS and JS link.
Something like this
<script type="text/javascript">
swaltitle="Success";
swaltype="success";
swaltext="your message";
swallocation="xyz?id="+<?php echo $order_id?>;
confirmButtonText="Proceed";
</script>
回答5:
Something like this
This worked for me shortest way include css and js and after that put this code.
echo "<script>
swal('Error!', 'You have tried too many invalid Attempt !',
'error', {closeOnClickOutside: false,});
</script>";
来源:https://stackoverflow.com/questions/28877566/echo-with-sweetalert-not-working