0

私は単純なログインシステムを持っていますが、資格情報を正しく入力していないときにエラーメッセージをエコーアウトする代わりに、メッセージを入力し<p></p>たり、同様のものを入れたりしたいと考えています。空の配列を使用してみました

$data = array();

そして、エコーを使用する代わりに、エラーが発生するたびにステータス変数を設定します

$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 -->
4

1 に答える 1

1
$data["status"] = "...."
and on the html part i put in

<?php if ( isset($status) ) : ?>
    <p><?= $status; ?></p>
<?php endif; ?>

私が見る限り、$status設定されていません。$data["status"]が設定されています。

コメントで提案されているように、エラーメッセージを渡すための適切な方法に関しては。このエラーは、現在のセッションでのみ実際に関連する値であるため、 に渡すのが理にかなってい$_SESSIONます。また、すべての値に関連するタイトルを付けたいので$_SESSION['error'];、エラー メッセージを渡すのに完全に正当な方法です。

于 2013-03-06T13:47:35.757 に答える