如何获取下拉选择值并将其发送到codeigniter中的控制器

时间:2013-09-27 07:17:52

标签: codeigniter

我有以下代码

视图

<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');
    }

这里我如何获得下拉选择值取决于我打开下一页所需的语言。请有人帮帮我,谢谢。

3 个答案:

答案 0 :(得分: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;
    }

答案 1 :(得分: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)以及即将发生的响应。

答案 2 :(得分:0)

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

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

同样在您的控制器中,您可以使用

验证ajax请求
$this->input->is_ajax_request()