我这是个登陆页面,可以正常登陆了。不过当我在用户名和密码中,乱输入后,按登录按钮,弹出一个乱码提示框和登录失败提示框。大侠们帮帮忙了。到底哪错了
$user_login=$_POST["user_login"];
$user_pass=$_POST["user_pass"];
if($user_login && $user_pass)
{
$query="select * from Background where user='".$user_login."' and pwd='".$user_pass."'";
$res=mysql_query($query,$connection);
if(mysql_fetch_array($res))
{
$_SESSION['user_login']=$user_login;
$_SESSION['user_pass']=$user_pass;
header('Location:index_bc.htm');exit;
}else{
echo "<script>alert('登录失败');</script>";
}
}
?><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>hotel</title>
<script type="text/javascript">
function Checkform()
{ if(document.form1.user_login.value=="")
{
alert("用户名密码不能为空");
document.form1.user_login.focus();
return false;
}
else if(document.form1.user_pass.value == "")
{
alert("用户名或密码错误");
document.form1.user_pass.focus();
return false;
}
else
{
return true;
}
}</script>
</head><body>
<p> </p><form name="form1" method="post" action="" onsubmit="return Checkform();">
<table width="289" border="1" cellspacing="0" cellpadding="1" bordercolor="#999999" align="center">
<tr bgcolor="#999999">
<td>
<div align="center"><font color="#FFFFFF" size="4">hotel登录</font></div>
</td>
</tr>
<tr>
<td>
<table width="267" border="0" cellspacing="0" cellpadding="5" align="center">
<tr>
<td width="94">
<div align="right">用户名:</div>
</td>
<td width="169">
<input type="text" name="user_login" size="16" maxlength="16" >
</td>
</tr>
<tr>
<td width="94">
<div align="right">密码:</div>
</td>
<td width="169">
<input type="password" name="user_pass" size="16" maxlength="16">
</td>
</tr>
<tr>
<td colspan="2">
<div align="center">
<input type="submit" name="login" value="登录 ">
</div>
</td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</body>
</html>
$user_login=$_POST["user_login"];
$user_pass=$_POST["user_pass"];
if($user_login && $user_pass)
{
$query="select * from Background where user='".$user_login."' and pwd='".$user_pass."'";
$res=mysql_query($query,$connection);
if(mysql_fetch_array($res))
{
$_SESSION['user_login']=$user_login;
$_SESSION['user_pass']=$user_pass;
header('Location:index_bc.htm');exit;
}else{
echo "<script>alert('登录失败');</script>";
}
}
?><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>hotel</title>
<script type="text/javascript">
function Checkform()
{ if(document.form1.user_login.value=="")
{
alert("用户名密码不能为空");
document.form1.user_login.focus();
return false;
}
else if(document.form1.user_pass.value == "")
{
alert("用户名或密码错误");
document.form1.user_pass.focus();
return false;
}
else
{
return true;
}
}</script>
</head><body>
<p> </p><form name="form1" method="post" action="" onsubmit="return Checkform();">
<table width="289" border="1" cellspacing="0" cellpadding="1" bordercolor="#999999" align="center">
<tr bgcolor="#999999">
<td>
<div align="center"><font color="#FFFFFF" size="4">hotel登录</font></div>
</td>
</tr>
<tr>
<td>
<table width="267" border="0" cellspacing="0" cellpadding="5" align="center">
<tr>
<td width="94">
<div align="right">用户名:</div>
</td>
<td width="169">
<input type="text" name="user_login" size="16" maxlength="16" >
</td>
</tr>
<tr>
<td width="94">
<div align="right">密码:</div>
</td>
<td width="169">
<input type="password" name="user_pass" size="16" maxlength="16">
</td>
</tr>
<tr>
<td colspan="2">
<div align="center">
<input type="submit" name="login" value="登录 ">
</div>
</td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</body>
</html>
没有写这个数据库连接吧?
echo "<script>alert('登录失败');</script>";可以在前面赋值一个$message='';登陆失败赋值为上面的js语句如果$message不为空,则在页面后面输出$message
但从编码上来说,JS弹出的信息也不会是乱码。
当前这个页面,就没有地方会弹出信息了。
你只有检查下,你所包含的文件,看什么位置弹出了信息,且那个页面的编码也不对。
$user_pass=$_POST["user_pass"];
$message="<script>alert('登录失败');window.location='in.php';</script>";
if($user_login && $user_pass)
{
$query="select * from background where user='".$user_login."' and pwd='".$user_pass."'";
$res=mysql_query($query,$connection);
if(mysql_fetch_array($res))
{
$_SESSION['user_login']=$user_login;
$_SESSION['user_pass']=$user_pass;
header('Location:index_bc.htm');exit();
}else{
if (isset($message)){
echo $message;
}
}
}