更好的替代jquery .change?

时间:2012-04-27 15:08:14

标签: javascript jquery

我在使用正确的方法“解雇”一段剧本时遇到了一些麻烦。

我创建了一个登录脚本,可以验证用户,而无需重新加载整个页面。 我用jQuery发布的一段JavaScript完成了这个。 登录部分正在运行,但另一件事情不是。

我希望我的菜单栏在用户成功登录时更新,这是通过重新加载文件menu.php完成的,该文件每次加载时都会检查“userlevel”并显示用户有权查看的所有链接。

登录脚本的可见部分包含在DIV标记中,当用户成功登录时,此DIV的内容将更改为“欢迎用户”文本和注销链接。

我的菜单也包含在DIV标签中。

我已经设置了我的“更新菜单”脚本以使用jQuery .change函数,但这不起作用,因为.change似乎在查看表单而不是我的DIV内容。

有更好的方法吗? (可能,但是我只是个初学者)

也许代码会更好地解释:

我的JS文件(main.js)

$(document).ready(function(){

$("form#loginform").submit(function()
    {

        var FormUserName;
        FormUserName = $("input#loginform_username").val();

        var FormPassword;
        FormPassword = hex_md5(document.getElementById('loginform_password').value);
        document.getElementById('loginform_password').value = "";

        var LoginOK;
        LoginOK = new XMLHttpRequest();
        LoginOK.open("POST","login.php?req=validate",true);
        LoginOK.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        LoginOK.send("username=" + FormUserName + "&password=" + FormPassword);

        LoginOK.onreadystatechange = function()
        {
            if (LoginOK.responseText == "ok")
                {
                    var response;
                    response = new XMLHttpRequest();
                    response.open("GET","login.php?req=loginOK",true);
                    response.send();
                    response.onreadystatechange = function()
                    {
                        if (response.readyState == 4 && response.status == 200)
                        {
                            document.getElementById("login").innerHTML = response.responseText;
                        }
                    }
                }
            if (LoginOK.responseText == "failed") 
                {
                    document.getElementById("loginresponse").innerHTML = "<font color=\"red\">Login Failed, try again</font>";
                }
        }
        return false;
    });

$("div#login").change(function()
    {
        $('#menu').fadeOut('slow').load('menu.php').fadeIn("slow");
    });
});

我的PHP登录脚本:(login.php)

<?php
include_once ("dbsetup.php");

if (!isset($_SESSION['status']))
    {
        session_start();
        $_SESSION['status'] = "started";
    }

function validate($user, $pass)
    {
        $result = mysql_query("SELECT username, password, userlevel FROM users WHERE username = '$user'");
        if (mysql_num_rows($result) > 0)
            {
                    $row = mysql_fetch_array($result);
                    $correctuser = $row['username'];
                    $correctpass = $row['password'];
                    $userlevel = $row['userlevel'];

                if ($user == $correctuser && $pass == $correctpass)
                    {
                        $_SESSION['username'] = $user;
                        $_SESSION['userlevel'] = $userlevel;
                        echo "ok";
                        exit;
                    }
                else 
                    {
                        echo "failed";
                        exit;
                    }
            }
        else
            {
                echo "failed";
                exit;
            }
    }

if (isset($_GET['req'])) $req = $_GET['req'];
else $req = NULL;

switch ($req)
    {
        case validate:
            $user = strtolower($_POST['username']);
            $pass = $_POST['password'];
            validate($user, $pass);
            exit;
            break;

        case loginOK:
            echo "<font id=\"welcomeuser\">Welkom " . $_SESSION['username'] . "</font> <br /> <br />";
            echo "<a id=\"logout\" href=\"login.php?req=logout\">Uitloggen</a>";
            exit;
            break;

        case logout:
            session_destroy();
            header('Location: index.php');
            exit;
            break;

        default:
            if (!isset($_SESSION['username']))
                {
                    ?>
                        <form id="loginform">
                            <table border="0">
                                <tr> <td>Gebruiker: </td> <td> <input id="loginform_username" name="username" type="text" placeholder="Gebruikersnaam ..." /> </td> </tr>
                                <tr> <td>Wachtwoord: </td> <td> <input id="loginform_password" name="password" type="password" placeholder="Wachtwoord ..." /> </td> </tr>
                                <tr> <td> <input id="loginform_submit" type="submit" value="Inloggen" /> </td> <td id="loginresponse"> </td> </tr>
                            </table>
                        </form>
                    <?php
                }
            else
                {
                    echo "<font id=\"welcomeuser\">Welkom " . $_SESSION['username'] . "</font> <br /> <br />";
                    echo "<a id=\"logout\" href=\"login.php?req=logout\">Uitloggen</a>";
                    exit;
                    break;
                }

    }

&GT;

PHP主页(index.php)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd” &GT;

<head>
    <title> Test </title>
    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript" src="jshash/md5-min.js"></script>
    <script type="text/javascript" src="main.js"></script>
    <link rel="stylesheet" type="text/css" href="style.css" />
</head>

<body>

    <div id="login">
        <?php include("login.php"); ?>
        <br />
    </div>

    <div id="menu">
        <?php include ("menu.php"); ?>
    </div>
</body>

最后但并非最不重要的是,PHP菜单(menu.php)

<?php

include_once("dbsetup.php");

if (isset($_SESSION['userlevel'])) $userlevel = $_SESSION['userlevel']; // Get userlevel from _SESSION variable if it exists, when userlevel is not set in _SESSION variable assume guest (userlevel 0)
else $userlevel = 0;
echo $userlevel;

$result = mysql_query("SELECT item_name, item_link, userlevel FROM menuitems WHERE userlevel <= $userlevel ORDER BY userlevel, item_order"); // Get menu items for the current userlevel or lower

while ($row = mysql_fetch_array($result)) // Now we loop through the results from our query and display the menu items the user has the right to see
    {
        echo "&nbsp;||&nbsp;<a class=\"lvl" . $row['userlevel'] . "\" href=\"" . $row['item_link'] . "\">" . $row['item_name'] . "</a>";
    }
echo "&nbsp;||"; // display the last item divider as closure

&GT;

0 个答案:

没有答案