日期:2014-05-17  浏览次数:20367 次

该如何清空$_POST里的数据?
试写了一个登陆界面,还没有完成,测试的时候发现问题。$_POST里的值不会清空,每输入一次用户名,$_POST里面就多一个用户名,后果就是第一次错误输入的话,就无法再正确输入用户名。。。除非浏览器刷新页面。。。



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="en-us" http-equiv="Content-Language" />
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Welcome back</title>
<style type="text/css">
.auto-style2 {
text-align: center;
}
.auto-style1 {
font-size: 36pt;
text-align: left;
}
.auto-style4 {
font-family: Broadway;
font-size: xx-large;
}
</style>
</head>

<body>

<table align="center" style="width: 100%">
<tr>
<td>
<div class="auto-style2">
<table align="center" style="width: 100%">
<tr>
<td class="auto-style1">Welcome back</td>
<td>
<img height="89" src="logo.jpg" style="float: right" width="184" /></td>
</tr>
</table>
</div>
<hr />
<p class="auto-style4">Log in:</p>
<form action="login.php" method="post" style="height: 213px">
<div class="auto-style2">
<table style="width: 100%">
<tr>
<td class="auto-style2">User Name:</td>
</tr>
<tr>
<td class="auto-style2">
<input name="user_name" type="text" value="
<?php 
if(isset($_POST['user_name']))
{
print htmlspecialchars($_POST['user_name']);
}
?>
"/>
</td>
</tr>
<tr>
<td class="auto-style2">Password:</td>
</tr>
<tr>
<td class="auto-style2">
<input name="password" type="password" /></td>
</tr>
</table>
<br />
<br />
<input name="submit" type="submit" value="Sign in" /></div>
</form>
</td>
</tr>
</table>

<?php

require_once 'connectDB.php';

print '<style type = "text/css" media = "screen">.error {color: red;}</style>';

if($_SERVER['REQUEST_METHOD']=='POST')
{
$problem = false;
$login = false;

if(empty($_POST['user_name']))
{
$problem = true;
print '<p class = "error">Please enter your user name!</p>';
}
if(empty($_POST['password']))
{
$problem = true;
print '<p class = "error">Please enter a password</p>';
}

if(!$problem)
{
$user = trim(stripslashes($_POST['user_name']));
$pwd = trim(stripslashes($_POST['password']));
$user = mysql_real_escape_string($user);
$pwd = mysql_real_escape_string($p