Ajax登录框没有';t调用PHP文件以继续处理


Ajax login box doesn't call PHP file to continue process

用于登录和验证的Ajax函数似乎无法访问.php文件进行验证。

我对JS和Ajax都是新手,并遵循了一些在线教程,然后我尝试用我已经掌握的东西来实现它,问题就开始了。

我在Ajax调用的页面顶部放了一个echo,它永远不会显示。我已经输入了网址以确定,它工作得很好。(显示回波)

我已经看了几次代码,没有看到任何明显的错误,但我不完全确定我应该寻找什么。

如果我把PHP留在HTML页面的"页眉"中,它可以正常工作,但我知道这是一种糟糕的做法。感谢您的帮助。

HTML:

 <form method="post" action="" id="ourLoginFormID_JS">
                    <div class="ourContactFormElement2">
                        <label for="username">Username:</label>
                        <input type="text" id="username"  name="username" autocomplete="off" class="required" value="<?php if(isset($_POST['username'])) echo htmlentities($_POST['username']); ?>"  />
                    </div>
                    <div class="ourContactFormElement2">
                        <label for="password">Password:</label>
                        <input type="password" id="password" name="password" autocomplete="off" class="required"/>
                    </div>
                    <div class="ourContactFormElement2">
                        <label> &nbsp; </label>
                        <input type="submit" name="loginButton" id="loginButton" value="Login!" onclick="validLogin()"/>
                    </div>
                    <div id="statusLogin"></div>
                </form>

Ajax:

function validLogin(){
$('.error').hide();
var username = $('#username').val();
if(username == ""){
    $('label#usernameError').show();
    $('input#username').focus();
    return false;
}
$('.error').hide();
var password = $('#password').val();
if(password == ""){
    $('label#passwordError').show();
    $('input#password').focus();
    return false;
}
var params = "username="+username+"&password="+password;
var url = "loginProcessAjax.php";
$("#statusLogin").show();
$("#statusLogin").fadeIn(400).html('<img src="image/loading.gif" />');
$.ajax({
    type: 'POST',
    url: url,
    dataType: 'html',
    data: params,
    beforeSend: function() {
        document.getElementById("statusLogin").innerHTML= 'checking...'  ;
    },
    complete: function() {
    },
    success: function(html) {
        $("#statusLogin").hide();
        document.getElementById("statusLogin").innerHTML= html;
        if(html=="success"){
            window.location = "index.php"
        }
    }
});
}

PHP-我知道这个页面上可能有一些冲突的问题,但它并没有走那么远,但是如果你有任何指针,那也会很棒。

<?php
//check fields are filled in
echo "HULLOOOO!!!!!"; // doesn't display when run in function but ok from url
if(empty($_POST) === false){
$username = trim($_POST['username']);
$password = trim($_POST['password']);
//validate fields
if(empty($username) === true || empty($password) === true){
    $errors[] = 'Your username and password are needed.';
}else if($users->userExists($username) === false){
    $errors[] = 'That username does not exist';
}else if($users->emailActivated($username) === false){
    $errors[] = 'You need to activate the account, please check your email.';
}else{
    //start login
    $login = $users->login($username, $password);
    if($login === false){
        $errors[] = 'That username or password is invalid';
//
//            if(empty($errors) === false){
//                echo '<p>' .implode('</p><p>', $errors).'</p>';
//            }
    }else{
        //destroy old session and create new - prevents session fixation attacks
        session_regenerate_id(true);
        //all details are correct - the method returns the id to be sotred as a session
        $_SESSION['id'] = $login;
        //send user to their home page
        echo "success";
        header('Location: userHome.php');
        exit();
    }
}
}

post请求的数据格式不正确。使用{'property1': 'value1', etc}

首先尝试使用PHP显示数据,当您确定jQuery和PHP之间的连接运行良好时,使其更加复杂。

使用Firebug或开发人员工具查看是否出现错误。