我在我已加载的页面上输出错误消息时遇到问题。按下登录按钮后,我想检查输入信息,如果不正确,则在不重新加载页面的情况下返回特定位置的错误。是否有可能在元素内部回声(例如我在下面尝试做的div中)?
<?php
if (isset($_POST['login'])) { login(); }
function login(){
if(isset($_POST['username'])&&isset($_POST['password'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$password_hash=md5($password);
if(!empty($username)&&!empty($password)) {
//stuff
} else {
$loginerror = "You must supply a username and password";
}
}
}
?>
<div style="z-index: 10; float:right; margin-top: -20;">
<h3>Already a user? Log in:</h3>
<div><?php if (isset($loginerror)){echo $loginerror;} ?></div>
<form action="<?php echo $current_file; ?>" method="POST">
<div style="float: left; width: 80px;">Username:</div> <input type="text" name="username"> <br />
<div style="float: left; width: 80px;">Password:</div> <input type="password" name="password">
<input type="submit" value="Log In" name="login">
</form>
</div>
答案 0 :(得分:1)
因为您没有向登录功能提供表单数据,所以它无法验证任何内容。我能够修改你的代码并使其更小,但它没有使用登录功能。
<?php
$loginerror = "";
if(isset($_POST['login'])) {
if( !empty($_POST['username']) && !empty($_POST['password']) ) {
//stuff
}
else {
$loginerror = "You must supply a username and password.";
}
}
?>
<div style="z-index: 10; float:right; margin-top: -20;">
<h3>Already a user? Log in:</h3>
<div><?php echo $loginerror; ?></div>
<form action="" method="POST">
<div style="float: left; width: 80px;">Username:</div> <input type="text" name="username"> <br />
<div style="float: left; width: 80px;">Password:</div> <input type="password" name="password">
<input type="submit" value="Log In" name="login">
</form>
</div>
现在,只要您需要向用户提供登录错误,只需使用$loginerror = "error here";