【问题标题】:Yii2 : Getting values from pop up in controllerYii2:从控制器中的弹出窗口获取值
【发布时间】:2016-01-19 11:44:57
【问题描述】:

我正在使用 yii2-advanced-app 并且想使用 post 方法将用户名和密码发送到我的 siteController。我不想在这里使用隐藏的表单域。

我是这样尝试的:

我在 index.php 中的弹出窗口是 -

<form id = "userlogin" action = "index.php?r=site/loginpopup" method = "POST">
                    <label>Email / Username</label>
                    <input type="text" name="username" id="username"/>
                    <br />

                    <label>Password</label>
                    <input type="password" name="password" id="password"/>
                    <br />

                    <div class="checkbox">
                        <input id="remember" type="checkbox" />
                        <label for="remember">Remember me on this computer</label>
                    </div>

                    <div class="action_btns">
                        <div class="one_half"><a href="#" class="btn back_btn"><i class="fa fa-angle-double-left"></i> Back</a></div>
                        <div class="one_half last"><input type="submit" value="Search" onclick="getCred()"><a href="javascript: getCred()" name='login-button' class="btn btn_red">Login</a></div>
                    </div>
                </form>
                <script type="text/javascript">
                var userloginValidator = new Validator("userlogin");
                userloginValidator.addValidation("username","req", "Please enter the value for query");
                function getCred(){
                    var un = document.getElementById('username').value;
                    var pwd = document.getElementById('password').value;
                    alert(un);alert(pwd);
                    // window.location = "index.php?r=site/loginpopup&username="+un+"&password="+pwd;
                    if(document.userlogin.onsubmit()) {
                        document.userlogin.submit();
                    }
                    $.post("index.php?r=site/loginpopup", { username: un }, { password: pwd });
                    // jQuery.post('index.php?r=site/loginpopup',{ username: un }, { password: pwd });
                }
                </script>

而控制器的功能是——

public function actionLoginpopup()
    {
$un = $_POST['username'];
echo $un;
        exit();
}

但是,它说——

错误请求 (#400) 无法验证您提交的数据。

【问题讨论】:

    标签: php post http-post yii2-advanced-app


    【解决方案1】:

    我认为您不能使用 jquery post 方法发布多部分表单数据

    尝试使用 Yii2 形式,并把这一行

    'enctype' => 'multipart/form-data' 在这样的表单选项中:

        $form = ActiveForm::begin([
        'id'      => 'userlogin',
        'method'  =>'post',
        'action'  => Url::toRoute['site/loginpopup'],
        'options' => [
            'enctype'          => 'multipart/form-data'
        ],
    ]); ?>
    

    YiiForm

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-07
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-14
      相关资源
      最近更新 更多