如何解决PHP重定向后使用sweetalert
登录失败后,我想使用Sweetalert。无法正常工作。我使用正确的CDN吗?
login.php
if(password_verify($passwordInput,$dbPasswd)) {
header('Location: ../store.php');
} else {
header('Location: ../index.php?login=false'); //redirect to index.php and show sweetalert
}
index.php:
<head>
<script src="jquery-3.3.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/2.1.2/sweetalert.min.js"></script>
</head>
if(isset($_GET['login']) == 'false') {
echo '<script type="text/javascript">$(document).ready(function() {
swal({
title: "Your title",text: "whatever",type: "error"
});
});</script>';
}
解决方法
通常在发出Ajax请求时,我会通过这种语法使用甜蜜警报。这是示例:
$.ajax({
type: "PATCH",url: "myurl.php",contentType: "application/json",data: data,success: function (data) {
swal({
title: 'Success',text: 'whatever',type: 'success'
});
},error: function (xhr,error) {
swal({
title: xhr.responseJSON.title,text: xhr.responseJSON.detail,type: 'error'
});
}
});
因此,在您的情况下,您应该具有以下内容:
<head>
<script src="jquery-3.3.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/2.1.2/sweetalert.min.js"></script>
</head>
<?php
if(isset($_GET['login']) == 'false') {
?>
<script type="text/javascript">
$(document).ready(function() {
swal({
title: 'Your title',type: 'error'
});
});
</script>
<?php
}
?>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。