在过程

时间:2018-01-08 03:37:02

标签: javascript php sweetalert

我想问一下,如何在process.php中调用甜蜜警报?

例如,如果在html中:

<!DOCTYPE html>
<html>
<head>
<title>Sweet Alert</title>
<link rel="stylesheet" type="text/css" href="plugins/sweetalert/sweetalert.css">
<script type="text/javascript" src="plugins/sweetalert/sweetalert.min.js"></script>
</head>
<body>
<button onclick="sweet()">Sweet Alert</button>
<script>
function sweet (){
swal("Good job!");
}
</script>
</body>
</html>

enter image description here

好吧,如果放入process.php怎么办?

示例有两个字段:

tes.php:

<form method="post" action="proses.php">
<label>NIK</label><br>
<input type="text" name="nik"><br>
<label>Confirm NIK</label><br>
<input type="text" name="confirmnik"><br><br>
<button type="submit" name="submit">Cek</button>
</form>

process.php:

<?php
    $nik        = $_POST['nik'];
    $confirmnik = $_POST['confirmnik'];

    if ($nik<>$confirmnik)
    {
        echo "<script language='javascript'>alert('nik and confirm nik do not match !');window.history.back();</script>";
    }
    else
    {
        echo "<script language='javascript'>alert('ok');window.history.back();</script>";
    }
?>

结果:

enter image description here

如何使用process.php文件中的甜蜜警报更改javascript默认警报?

1 个答案:

答案 0 :(得分:1)

您应该可以使用alert无缝替换swal

您唯一的问题是调用window.history.back函数。您需要将其置于回调中,因为甜蜜警报不会像alert那样阻止线程。

您可以使用promises

所以你的JS代码看起来像:

swal('nik and confirm nik do not match !').then(() => {window.history.back()});

完整的PHP代码如下所示:

<?php
    $nik        = $_POST['nik'];
    $confirmnik = $_POST['confirmnik'];

    if ($nik<>$confirmnik)
    {
        echo "<script language='javascript'>swal('nik and confirm nik do not match !').then(() => { window.history.back(); });</script>";
    }
    else
    {
        echo "<script language='javascript'>swal('ok').then(() => { window.history.back(); });</script>";
    }
?>