我的代码中存在if循环,而我没有将$ _GET ['err']分配给变量。
我刚刚登录到我的网页。我没有更改密码。未更改任何密码都会显示有关更改密码的错误消息。我是一个初学者,所以我没什么主意。
<body>
<div class="header">
<div class="logo">
<img src="images/l.png" alt="aczel logo">
</div>
<div class="nav">
<h1>aczel quality team</h1>
<ul>
<li class="active"><a href="#">Home Page</a></li>
<li onclick="c_p()" ><a href="#">change password</a></li>
</ul>
<!--[if !IE]> -->
<div id="mycp" class="cp">
<span class="cp_close"
onclick="document.getElementById('mycp').style.display='none'">×
</span>
<div>
<form action="user_cp.php " method="post">
<input type="password" name="old_pswd" placeholder="ENTER OLD PASSWORD">
<input type="password" name="new_pswd" placeholder="ENTER NEW PASSWORD">
<input type="password" name="c_pswd" placeholder="CONFIRM NEW PASSWORD">
<input type="submit" name="change_pswd" value="CHANGE PASSWORD">
</form>
</div>
</div>
<script type="text/javascript" src="js/modal.js">
</script>
<!-- <![endif]-->
</div>
</div>
<?php
/*displaying error message for empty password change*/
if($_GET['err'] == 1)
{
echo "<div class='msg'><h2>Fields are empty</h2></div>";
}
/*displaying error message for !match passwords*/
if($_GET['err'] == 2)
{
echo "<div class='msg'><h2>Old Password Does Not Match</h2></div>";
}
/*displaying error message for new password and confirm pwd dont match */
if($_GET['err'] == 3)
{
echo "<div class='msg'><h2>New Pwd != confirm Password</h2></div>";
}
/*displaying success message on password change*/
if($_GET['success'] == 1)
{
echo "<div class='msg'><h2>Password changed</h2></div>";
}
?>
这些错误消息在我们登录时不会出现。而是在我们使用网页中的密码更改选项时显示它们。
答案 0 :(得分:0)
您要在GET数组中请求不存在的值,而无需测试是否设置了它们-您需要在派生逻辑之前先进行测试-例如:
if( isset( $_GET['err'] ) ){
if($_GET['err'] == 1){
echo "<div class='msg'><h2>Fields are empty</h2></div>";
}
/*displaying error message for !match passwords*/
if($_GET['err'] == 2){
echo "<div class='msg'><h2>Old Password Does Not Match</h2></div>";
}
/*displaying error message for new password and confirm pwd dont match */
if($_GET['err'] == 3){
echo "<div class='msg'><h2>New Pwd != confirm Password</h2></div>";
}
/*displaying success message on password change*/
if( isset( $_GET['success'] ) && $_GET['success'] == 1){
echo "<div class='msg'><h2>Password changed</h2></div>";
}
}
或者,也许是更紧凑的版本
if( isset( $_GET['err'] ) ){
switch( intval( $_GET['err'] ) ){
case 1:$msg='Fields are empty';break;
case 2:$msg='Old Password Does Not Match';break;
case 3:$msg='New Pwd != confirm Password';break;
}
}
if( isset( $_GET['success'] ) && $_GET['success']==1 ){
$msg='Password changed';
}
printf('<div class="msg"><h2>%s</h2></div>',$msg);