Ajax PHP Response登录

时间:2015-05-09 00:45:26

标签: javascript php jquery mysql ajax

我尝试通过login.js和我的logindata.php通过我的login.php做出回复,但我似乎总是收到错误消息Something went wrong! Try again.(即使我写了正确的登录详细信息,我自己在login.js中的自定义消息

的login.php

<form name="loginform" id="loginform" autocomplete="off">   
    <div class="form-group">
        <input type="text" name="username" maxlength="12" class="form-control" placeholder="Username" id="username" required/>
    </div>
    <div class="form-group">
        <input type="password" name="password" maxlength="12" class="form-control" placeholder="Password" id="password" required/>
    </div>
    <input id="login" type="submit" class="btn btn-primary btn-block" value="Login"/>
    <a href="/register" class="btn btn-info btn-block">Register</a>
</form>

login.js

$(function () {
    var action = '';
    var form_data = '';
    $('#login').click(function () {
        action = $("#loginform").attr("action");
        form_data = {
            username: $("#username").val(),
            password: $("#password").val(),
            is_ajax: '1'
        };
        $('#login').keypress(function (e) {
            if (e.which == 13) { //Enter key pressed
                $('#login').click();
            }
        });
        $.ajax({
            type: 'POST',
            url: 'logindata.php',
            data: form_data,
            success: function (response) {
                if (response == 'success') {
                    $("#loginform").slideUp('slow', function () {
                        $("#message").html('<script>location.reload();</script><div class=\"alert alert-success\">Logged in. Reloading...</div>');
                    });
                } else {
                    $('#message').hide().html("<br/><div class=\"alert alert-danger\">Something went wrong! Try again.</div>").fadeIn('fast');
                }
                console.log(response);
            }
        });
        return false;
    });
});

logindata.php

<?php
if(!isset($_SESSION['uid'])){
    header("Location: /home")
}
if (!isset($_POST['submit'])){
    header("Location: /home")
} else {
    $mysqli = new mysqli("localhost","root","root","maplexeno");
    if ($mysqli->connect_errno) {
        echo "<p>MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_error}</p>";
        exit();
    }

    $username = $_POST['username'];
    $password = $_POST['password'];

    $sql = "SELECT * from accounts WHERE username LIKE '{$username}' AND password LIKE '{$password}' LIMIT 1";
    $result = $mysqli->query($sql);
    if (!$result->num_rows == 1) {
        $_SESSION['uid'] = $username;
        echo "success";
    } else {
        echo "false";
    }

    session_start();
}
?>      

1 个答案:

答案 0 :(得分:0)

您可能在日志中收到标头输出或session_start()错误。您必须将session_start()放在logindata.php脚本的最顶部,紧跟<?php后面,然后再放在其他任何位置。

<?php
session_start();

// rest of your code...

您可以尝试在login.js中执行以下操作,以查看生成的PHP错误,而无需转到您的日志文件。

success: function (response) {
alert(response);

如果问题不是session_start()

,这可以帮助您找到问题