使用PHP在登录页面上显示错误消息

时间:2018-03-31 15:53:39

标签: javascript php ajax

HTML

用户可以注册并登录该网站。

<div id="sign_in_bottom_border">
    <label class="sign_in_label">Your username is<span class='error'> *</span><br>
        <input id="username_login" type="text" name="username" placeholder="Username..." required>
    </label>
    <br>
    <br>
    <label class="sign_in_label">Your password is<span class='error'> *</span><br>
        <input id="password_login" type="password" name="password" placeholder="Password..." required>
    </label>
    <br>
    <input class="log_in_button" onclick="login()" type="submit" value="SIGN IN">
    <p id="ErrorMessages"></p>
</div>

AJAX

AJAX用于客户端和服务器之间的通信。

function loadContent() {
    var search = document.getElementById("search_engine").value;
    // Create request object 
    var request = new XMLHttpRequest();
    // Create event handler that specifies what should happen when server responds
    request.onload = function() {
        // Check HTTP status code
        if (request.status == 200) {
            // Get data from server
            var responseData = request.responseText;
            // Add data to page
            document.getElementById("product_grid").innerHTML = responseData;
        } else
            alert("Error communicating with server: " + request.status);
    }
    // Set up request with HTTP method and URL 
    request.open("POST", "php/search.php");
    request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    // Send request
    request.send("search=" + search);
}

PHP

服务器端脚本是用PHP编写的。

<?php
//Start session management
session_start();

//Get name and address strings - need to filter input to reduce chances of SQL injection etc.
$username = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_STRING);
$password = filter_input(INPUT_POST, 'password', FILTER_SANITIZE_STRING);    

//Connect to MongoDB and select database
$mongoClient = new MongoClient();
$db = $mongoClient->ecommerce;

//Create a PHP array with our search criteria
$findCriteria = [
    "username" => $username, 
    "password" => $password
];

//Find all of the customers that match  this criteria
$cursor = $db->customers->find($findCriteria);

//Get customer
$customer = $cursor->getNext();

//Check username and password
if($customer['username'] == "") {
    echo 'Please enter your username.';
    return;
}
else if($customer['password'] == "") {
    echo 'Please enter your password.';
    return;
}
else if($customer['username'] != $username) {
    echo 'Username incorrect.';
    return;
}
else if($customer['password'] != $password) {
    echo 'Password incorrect.';
    return;
}
else {
    //Inform web page that login is successful
    echo 'Log in successful!';
}

//Start session for this user
$_SESSION['loggedInUser'] = $username;  

//Close the connection
$mongoClient->close();
?>    

AJAX代码可以工作,但PHP代码不能按照我想要的方式工作。每当文本字段为空时,我点击登录按钮,它会显示“请输入您的用户名”。每当文本字段不为空时,它仍会显示相同的输出 - “请输入您的用户名”。我该如何解决这个问题?

0 个答案:

没有答案
相关问题