该如何清空$_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 src="/static/imghw/default1.png" data-src="logo.jpg" class="lazy" style="max-width:90%" style="float: right" style="max-width:90%" / alt="该如何清空$_POST里的数据?" ></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><?phprequire_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($pwd); $q = "SELECT * FROM USER WHERE USER_NAME = '$user' and PASSWORD = '$pwd'"; echo $q; $result = mysql_query($q); if(!$result || mysql_num_rows($result) == 0) { print '<p class = "error">Wrong Username or Password.</p>'; } else { $row = mysql_fetch_array($result); $id = $row['USER_ID']; $name = $row['USER_NAME']; echo "User ID is ".$id; session_start(); $_SESSION['$id'] = $id; //clear the POST array $_POST = array(); //网上找来的办法, 但貌似不管用 } } else { print '<p class = "error">Please try again!</p>'; } }?></body></html>
请高手帮忙解决一下,感激不尽
回复讨论(解决方案)
<input name="user_name" type="text" value=" <?php if(isset($_POST['user_name'])) { print htmlspecialchars($_POST['user_name']); } ?> "/>
<input name="user_name" type="text" value="<?php if(isset($_POST['user_name'])) { print htmlspecialchars($_POST['user_name']); } ?>"/>
是 value 中的空格和回车换行符在作怪
PHP code?12345678 谢谢版主大神。小弟刚刚学习php和网页制作,大神是否能解释一下,空格和回车符作怪的原因吗?
input type=text 是单行文本编辑器
如果你给他的是多行的文本,那么只有第一行可以被编辑。这就是你遇到的问题的原因
怎么会有这种事???
=================================================
我是来学习的
可能是 你post 赋值时有问题,post 的值只会覆盖上一个,怎么会附加呢..

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.
