我有一个简单的登录系统,但是当他们没有正确输入凭据时,我不想回显错误消息,而是希望能够将消息放入<p></p>
或类似的东西中。我尝试使用空数组
$data = array();
然后在出现错误时设置状态变量而不是使用 echo
$data["status"] = "...."
在我输入的 html 部分
<?php if ( isset($status) ) : ?>
<p><?= $status; ?></p>
<?php endif; ?>
但我似乎无法让它工作。我也试过:
extract($data)
但是我再次显然做错了。我不确定这是否是正确的方法。使用 javascript 或 jquery 会是处理这个问题的一种投注方式吗?无论如何这是我的代码。
$conn = DBconnect($config);
//create and empty array so we can ifor the users.
$data = array();
// if the user has submitted the form
if( $_SERVER["REQUEST_METHOD"] === "POST") {
//protect the posted value then store them to variables
$username = protect($_POST["username"]);
$password = protect($_POST["password"]);
//Check if the username or password boxes were not filled in
if ( !$username || !$password ){
// if not display an error message.
$data["status"] = "You need to fill in a username and password!";
}else{
//check if the username and password match.
$stmt = query("SELECT * FROM users WHERE username = :username AND password = :password",
array("username" => $username, "password" => $password),
$conn);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if(!$row){
//display an error message
$data["status"] = "Username or Password is incorrect.";
}elseif($row["active"] !=1) {
$data["status"] = "You have not activated your account!";
}else{
//we log the user in.
这一直在进行。这是 html 部分
<div class="container">
<form class="form-signin" action ="" method = "post">
<h2 class="form-signin-heading">Please Log In</h2>
<input type="text" class="input-block-level" name = "username" id = "username" placeholder="Username">
<input type="password" class="input-block-level" name = "password" id = "password" placeholder="Password">
<label class="checkbox">
<input type="checkbox" value="remember-me"> Remember me
</label>
<input type="submit" class = "btn btn-large btn-primary" name = "submit" value="Log in!" >
<?php if ( isset($status) ) : ?>
<p><?= $status; ?></p>
<?php endif; ?>
</form>
</div> <!-- /container -->