0

我有以下代码

看法

<script>
$(document).ready(function() 
            {
                $("#Login").click(function() 
                {   
                    $("#message").html("");
                    var action = $("#loginform").attr('action');
                    var login_data = {
                        username: $("#txt_email").val(),
                        password: $("#txt_password").val(),
                        language: $("#language").val(),
                        is_ajax: 1
                    };
                    $.ajax(
                    {
                        type: "POST",
                        url: '<?php echo base_url();?>survey/login',
                        data: login_data,
                        success: function(response)
                        {   
                            if(response == 'success')  
                            {
                                $("#message").html("<span class='success'>You have logged in successfully!</span><p>Redirecting...</p>");
                                setTimeout(function() {
                                window.location.href = '<?php echo base_url();?>survey/communication_letter';
                                }, 2000);
                            }
                            else 
                            {
                                $("#message").html("<span class='error'>OOPS! Authentication failed</span>");   
                            }
                        }
                    });
                    return false;
                });
            });
</script>
</head>

<body>
    <div id= "header">
    <br/>
            <p style="text-align:center; color:#8D0F1D;font-size:28px" >Work Environment Survey</p>

     </div>
    <div id= "bar" style="z-index:1">
     <div id="logo" style="z-index:2">

     </div>


     </div>
     <br/>
    <br/>


     <div id="homemain">

        <!--div id="content-login"-->
        <br/><br/><br/>
        <form action="#" id="loginform" method="post">
                    <table border="0" align="center" cellpadding="5" cellspacing="10" >
                        <tr>
                            <td>Email Id </td>
                            <td><input type="text" id="txt_email" name="username"  /></td> 
                        </tr>
                        <tr>
                            <td>Password </td>
                            <td><input type="password" id="txt_password" name="password" /></td> 
                        </tr>
                        <tr>
                            <td>Select Language</td>
                            <td><select style="width:215px" name="language" id = "language" ><option value="simplified">English with Simplified Chinese</option>
                            <option value="traditional">English with Traditional Chinese</option>
                            </select></td> 
                        </tr>
                    </table>
                    <input type="image" id="Login" style="position:relative;left:120px" src="<?php echo base_url();?>images/login.png"/>
                </form>

控制器如下

public function login()
    {   
        if(isset($_REQUEST['is_ajax']) && $_REQUEST['is_ajax']) {
            $username = $_REQUEST['username'];
            $password = $_REQUEST['password'];
            echo $verify = $this->user_model->user_login($username, $password);
            exit;
        }
        $this->load->view('login');
    }

在这里,我如何根据选择的语言获取下拉选择的值,我需要打开下一页。请有人帮助我,谢谢。

4

3 回答 3

0

我认为这是您想要的,当用户输入正确的凭据时,用户将根据用户选择的语言被重定向到页面。您不需要访问控制器中的语言,因为您正在从登录页面重定向,

你的控制器必须是

public function login()
{   
    if(isset($_REQUEST['is_ajax']) && $_REQUEST['is_ajax']) {
        $username = $_REQUEST['username'];
        $password = $_REQUEST['password'];
        $verify = $this->user_model->user_login($username, $password);
        if($verify){
          echo 1;
        }else{
           echo 0;
        }
    }
}

您不需要加载视图,因为它是一个 ajax 响应。

$.ajax(
                {
                    type: "POST",
                    url: '<?php echo base_url();?>survey/login',
                    data: login_data,
                    success: function(response)
                    {   
                        if(response == 1)  
                        {
                            $("#message").html("<span class='success'>You have logged in successfully!</span><p>Redirecting...</p>");
                            setTimeout(function() {
                             if($("#language").val() == "simplified"){
                          window.location.href = '<?php echo base_url();?>survey/communication_letter';
                             }else{
                           window.location.href = '<?php echo base_url();?>survey/trad_communication_letter'; }
                             }
                            }, 2000);
                         }
                        else 
                        {
                            $("#message").html("<span class='error'>OOPS! Authentication failed</span>");   
                        }
                    }
                });

我希望这能帮到您。请检查您的开发工具(chrome)或firebug(firefox)您的请求正在进行以及响应的内容。

于 2013-09-27T07:57:58.027 回答
0

从下拉列表中获取所选索引值的正确方法是

$('#language option:selected').val();

同样在您的控制器中,您可以使用验证 ajax 请求

$this->input->is_ajax_request()
于 2013-09-27T11:43:16.343 回答
0

你可以通过使用$_POST$_REQUEST像你对用户名所做的那样得到它

  if(isset($_REQUEST['is_ajax']) && $_REQUEST['is_ajax']) {
        $username = $_REQUEST['username'];
        $password = $_REQUEST['password'];
        $language= $_REQUEST['language'];

        echo $verify = $this->user_model->user_login($username, $password);
        exit;
    }
于 2013-09-27T07:20:43.717 回答