有人能向我解释一下为什么警报在我的代码中不起作用吗?我试图向用户显示警报&然后将他重定向到下一页。下面是代码:
<?php
$name=$_POST['name'];
$password=$_POST['password'];
if(isset($_POST['submit'])){
if($name == 'a' && $password == 'a'){
echo "<script>swal('Good job!', 'You clicked the button!', 'success');</script>";
}else{
echo 'Login Failed';
}
}
?>
<html>
<head>
<title>Organizer Login | Get My Fest</title>
<link rel="stylesheet" type="text/css" href="style.css">
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
<body>
<div class="loginbox">
<img src="avatar.png" class="avatar">
<h1>Login Here</h1>
<form method="post" action="">
<p>Username</p>
<input type="text" name="name" placeholder="Enter Username" required>
<p>Password</p>
<input type="password" name="password" placeholder="Enter Password" required>
<input type="submit" value="Login" name="submit">
</form>
</div>
</body>
</head>
</html>
发布于 2018-08-07 13:52:24
你有几个问题:
head
标记未正确关闭。它不应该包含body
但应该在它之前。echo
之前输出内容的指令。html
会显示出来,你至少应该在变量中分配字符串,然后在包含js文件之后输出它。body
,这是普通文本。就像这样:
<?php
$name=$_POST['name'];
$password=$_POST['password'];
$swal_result = '';
$text_result = '';
if(isset($_POST['submit'])){
if($name == 'a' && $password == 'a'){
$swal_result = "<script>swal('Good job!', 'You clicked the button!', 'success');</script>";
}else{
$text_result = 'Login Failed';
}
}
?>
<html>
<head>
<title>Organizer Login | Get My Fest</title>
<link rel="stylesheet" type="text/css" href="style.css">
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
<?php echo $swal_result; ?>
</head>
<body>
<?php if ($text_result != '') { ?>
<p><?php echo $text_result; ?></p>
<?php } ?>
<div class="loginbox">
<img src="avatar.png" class="avatar">
<h1>Login Here</h1>
<form method="post" action="">
<p>Username</p>
<input type="text" name="name" placeholder="Enter Username" required>
<p>Password</p>
<input type="password" name="password" placeholder="Enter Password" required>
<input type="submit" value="Login" name="submit">
</form>
</div>
</body>
</html>
https://stackoverflow.com/questions/-100001982
复制相似问题