0

我有登录的新主页并使用未登录用户注册。主页.php

<script type="text/javascript" >
            $(document).ready(function()
            {
                $("#btnLogin").click(function()
                {                    
                    var username= $("#usernamelogin").val();
                    var password=$("#passwordlogin").val();
                    var remember = $("#rememberlogin").is(':checked'); 
                    var dataString = 'usernamelogin='+ username + '&passwordlogin=' + password + '&rememberlogin=' + remember;
                    $.ajax
                    ({
                        type: "POST",
                        url: "homepage",
                        data: dataString,
                        cache: false,
                        success: function(html)
                        {
                            alert(html);
                        }

                    });
                });
            });


        </script>
<form action="" method="post" name="frmLogin" class="Login">
            <table width="960px" align="center">
                <tr>
                    <td rowspan="3" width="404"><label id="lbbanner"><b>UIT SOCIAL NETWORK</b></label></td>
                    <td width="216"><label>Username:</label></td>
                    <td width="324">Password:</td>
                </tr>
                <tr>
                    <td><input type="text" witdth="200px" id ="usernamelogin" name="txtUsernameLogin" /></td>
                    <td><input type="password" width="200px" id="passwordlogin" name="txtPasswordLogin" /></td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="checkbox" id="rememberlogin" name="checkRemember" />&nbsp;Remember me!
                        <input type="button" value="Log in" id="btnLogin" name="Login"/></td>
                    <td colspan="2" align="right"></td>

                </tr>
                <tr>

                </tr>
            </table>
        </form>

在 SiteController 我有 actionHomePage()

 public function actionHomePage() {
        $model = new LoginForm();      
        $model->username = $_POST['usernamelogin'];
        $model->password = $_POST['passwordlogin'];
        $model->rememberMe = $_POST['rememberlogin'];
        //echo $model->errors;
        print_rcount(($model->getErrors()));
        //echo $model->username . "&&" . $model->password . "$$" .$model->rememberMe;
        // validate user input and redirect to the previous page if valid        
        if ($model->validate() && $model->login()) {
            $this->loginStatus = true;
            //$this->redirect(Yii::app()->user->returnUrl);
            echo "SUCCESS";
        } else {

            echo "Error";
        }
    }

当我用正确的信息填写用户名和密码但它返回“错误”时,$model->validate() 是错误的,但我有正确的信息。我不知道我在这部分有什么问题。

4

2 回答 2

1

您的输入变量名称需要与您查询的 POST 变量匹配

<td><input type="text" witdth="200px" id ="usernamelogin" name="txtUsernameLogin" /></td>
<td><input type="password" width="200px" id="passwordlogin" name="txtPasswordLogin" /></td>

应该是这样的:

<td><input type="text" witdth="200px" id ="usernamelogin" name="usernameLogin" /></td>
<td><input type="password" width="200px" id="passwordlogin" name="passwordLogin" /></td>

记住登录也一样

于 2012-05-02T03:26:04.717 回答
-1

因为在 actionHomePage() 中:$model->rememberMe = true or false; $_post 返回一个字符串。你试试 :

($_POST['rememberlogin'] == "false") ? $model->rememberMe = false : $model->rememberMe = true ;
于 2013-04-17T18:00:17.103 回答