0

伙计们,使用以下代码:

$(document).ready(function() {
    $(".list-book-description").css("display", "none");
    $("#user-panel-login").css("display", "none");
    $('#login-form').submit(function(e) {
        e.preventDefault();
        var formUsername=$("#login-form #username").val();
        var formPassword=$("#login-form #password").val();
        if((formUsername.length<6))
        {
        $("#ajax-output").html("<div class='error'>Attenzione username troppo breve!</div>");
        }
        else if((formPassword.length<6))
        {
            $("#ajax-output").html("<div class='error'>Attenzione password troppo breve!</div>");
         }
    else
    {
        $.post(
            'index.php?module=login',
            {
                "username": formUsername,
                "password": formPassword,
            },
            function(data){
                $("#ajax-output").html(data);
            }
        );
    }
});
});

我试图将?index.php&module=login&function=1用户名和密码传递给#login-form字段#username和使用函数#passwordlogin()php 函数,setLogin();如果数据进入数据库来设置登录会话。

function Login()
 {
 if(!checkLogin())
    {
    $function=@stringEscape($_GET['function']);

    if($function==1)
    {
        $username=stringEscape($_POST['username']);
        $password=sha1(stringCrypt($_POST['password'], 'sha1').stringCrypt(DB_CRYPT, 'sha1'));
        if(setLogin($username, $password))
        {
            echo "<div class='ok'>Login effettuato con successo, tra pochi secondi verrai riportato alla homepage!</div>";
        }   
    }
}
else notfound();
}

function setLogin($username, $password, $time="")
{
if(!checkLogin())
{
    if((isset($username)) && (isset($password)))
    {
        $query=mysql_query("SELECT * FROM ".DB_PREF."users WHERE user_username='".$username."' AND user_password='".$password."'");
        if(mysql_num_rows($query))
        {
            $fetch=mysql_fetch_assoc($query);
            $_SESSION['logged_user_id']=$fetch['user_id'];
            $_SESSION['logged_user_username']=$fetch['user_username'];
            return true;
        }
        else
        {
            echo "<div class='error'>Dati non validi, ripeti la procedura di login o <a href='?index.php&module=registration'>registrati</a>!</div>";
        }
    }
}
else notfound();
}

现在当我$.post()在我的#ajax-inputdiv 中获得提交登录表单的结果时,我遇到了一个问题,它不仅返回登录函数的结果,而且返回索引页面+登录函数的结果。

如果我尝试将“json”参数设置为$.post函数并且echo json_encode()在 php 函数中它根本不起作用。那么,可能是什么问题?

4

1 回答 1

2

you make a request for a full page here, I think.you could make a parameter, post or get, you decide, and if that is for example true, then you return the full page, and he it's false you only return a JSON string or something like that. I believe it is even possible to determine if the request is from ajax or not! That could be your second option. Just add a simple if-statement, I think.

于 2012-04-16T19:52:19.410 回答