试写了一个登陆界面,还没有完成,测试的时候发现问题。$_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/imghwm/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 的值只会覆盖上一个,怎么会附加呢..

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

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

Laravel's service container and service providers are fundamental to its architecture. This article explores service containers, details service provider creation, registration, and demonstrates practical usage with examples. We'll begin with an ove

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version
Visual web development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
