我的登录jquery没有正确调用php文件

时间:2012-03-14 14:50:29

标签: php javascript jquery ajax login

嘿我正在尝试为登录系统编写代码,这是我第一次使用在线教程。初始Javascript工作正常,当输入保持为空时错误显示。但是,当我在输入中输入文本并单击登录时,没有任何反应。在我看来,它不是调用PHP文件,但我看不出任何理由为什么它不能正确引用?任何帮助将非常感谢!

<?php
    include ("Includes/dbConnect.php");
    $u=$_POST['u'];
    $p=$_POST['p'];

    //Strip slashes
    $u = stripslashes($u);
    $p = stripslashes($p);

    //Strip tags 
    $u = strip_tags($u);
    $p = strip_tags($p);


    $check = mysqli_query("SELECT * FROM Login WHERE login_user ='$u' AND login_pass='$p'")      or die(mysqli_error());
    $check = mysqli_num_rows($check);
    if($check !=="0"){
    $results = mysqli_query("SELECT * FROM Login WHERE login_user = '$u'") or die(mysqli_error());
        while ($row = mysqli_fetch_assoc($results)) {
            $login_user=$row['login_user'];
            session_register('login_user');
            $_SESSION['login_user'] = $Login_user;
            echo "1";
    }
    }
?>

这是javascript

<script type="text/javascript">
    $(document).ready(function () {
        $(".sign_b_btn").live("click", function () {
            var u = $("#u").val();
            var p = $("#p").val();

            if (u == "") {
                $("#u").css("border-color", "red");
                $("#un").css("color", "red");
                $(".error").show().html("Please enter your username!");
                $("#p").css("border-color", "#606060");
                $("#up").css("color", "#333333");
            } else if (p == "") {
                $("#u").css("border-color", "#606060");
                $("#un").css("color", "#606060");
                $(".error").show().html("Please enter your Password!");
                $("#p").css("border-color", "red");
                $("#up").css("color", "red");
            } else {

                dataString = 'u=' + u + '&p=' + p;
                $.ajax({
                    type: "POST",
                    url: "Ajax/login_php.php",
                    data: dataString,
                    cache: false,
                    success: function (html) {
                        if (html == "") {
                            $(".error").show().html("The username or password you entered is incorrect!");
                            $("#p,#u").css("border-color", "red");
                            $("#up,#un").css("color", "red");
                        } else if (html == "1") {
                            $(".error").fadeOut(1000);
                            $("#u").css("border-color", "#606060");
                            $("#un").css("color", "#333333");
                            $("#p").css("border-color", "#606060");
                            $("#up").css("color", "#333333");
                            $(".center").animate({
                                opacity: 0.25,
                                left: '+=900',
                                height: 'toggle'
                            }, 5000, function () {
                                $(".done").slideDown(200).html("Welcome " + u);
                                setTimeout(function () {
                                    var u = $("#u").val("");
                                    var p = $("#p").val("");
                                    window.location = "index.php";
                                }, 5000);
                            });
                        }
                    }
                });
            }
        });
    });
</script>

2 个答案:

答案 0 :(得分:0)

回答你的会话问题:

编辑您的php代码,以便通过调用session_start();

开始您的脚本

例如:

<?php session_start();
 //rest of your php code

替换行:

session_register('login_user');
$_SESSION['login_user'] = $Login_user;

使用:

$_SESSION['login_user'] = $Login_user;

你收到了你提到的错误,因为函数session_register();已在PHP 5.3.0版中弃用,并从PHP 5.4.0版中删除。 $ _SESSION是session_start()之后可用的全局数组;呼叫。

快乐编程:)

答案 1 :(得分:0)

感谢帮助人员,真的帮助我搞清楚了。如果感兴趣的话,完成的代码:

<?php

include ("../Includes/dbConnect.php");
$u=$_POST['u'];
$p=$_POST['p'];

//Strip slashes
$u = stripslashes($u);
$p = stripslashes($p);

//Strip tags 
$u = strip_tags($u);
$p = strip_tags($p);



$query = "SELECT * FROM Login WHERE login_user ='$u' AND login_pass='$p'";
$check = mysqli_query($cxn,$query) or die("Couldn't execute query!");
$check = mysqli_num_rows($check);
if($check !=="0"){
$results = mysqli_query($cxn,"SELECT * FROM Login WHERE login_user = '$u' AND login_pass='$p'") or die("Couldn't execute query2!");
    while ($row = mysqli_fetch_assoc($results)) {
        $username= $u;
        session_register('Login');
        $_SESSION['user'] = $username;
        echo "1";
}
}


?>