javascript无法使用codeigniter?

时间:2017-02-10 14:20:36

标签: javascript php jquery codeigniter

我只是想学习一些php framework.ans从codeigniter开始。所以你们可以理解我对codeigniter.i是全新的我只是在我的用户名和密码字段为空时显示错误消息。如果然后提交form.i想要用户和服务器端验证。问题是js没有显示错误消息,也没有提交。 这里是我的观点代码 - >

<body>
    <center>
        <h2>LOGIN</h2>
        <form method="post" name="loginForm">
            <table cellpadding="4" cellspacing="4">
                <tr>
                    <td>USERNAME</td>
                    <td><input type="text" name="username" id="iusername" /></td>
                </tr>
                <tr>
                    <td>PASSWORD</td>
                    <td><input type="password" name="password" id="ipassword" /></td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td><input onclick="checkInputFields()" type="button" value="Login" name="submit" /></td>
                </tr>
            </table>
        </form>
        <br />

        <!-- Label to display error message -->
        <?php
        if(!empty($errorMessage)){?>
            <label name="errorMessage" id="ierrorMessage"><?php echo $errorMessage;?></label>
        <?php
        }
        ?>

    </center>
</body>

表单正在从控制器页面进入,从中查看它。 我的js代码 - &gt;

<script type="text/javascript">
        function checkInputFields(){
            if(document.getElementById('iusername').value == "" || document.getElementById('ipassword').value == ""){
                document.getElementById('ierrorMessage').innerHTML('You cant left username and password field empty.');
            }
            else{
                //document.forms[0].submit();
                document.loginForm.submit();
            }
        }
    </script>

和我的控制器功能 - >

    public function index(){
    if($this->input->post('submit')){
        $uName=$this->input->post('username');
        $uPass=$this->input->post('password');

        if($uName == "" || $uPass == ""){
            $message['errorMessage']='You cant left username and password field empty.';
            $this->load->view('login',$message);
        }else{
            if($this->varifyuser->checkuser($uName,$uPass)){

            $this->session->set_userdata('loggedin', true);

            redirect(base_url()."home", 'refresh');

            }else{
                $message['errorMessage']='Invalid username or password';
                $this->load->view('login',$message);
            }
        }
    }
    else
    {
        $this->load->view('login');
    }
}

控制器功能工作过时fine.i检查,但js不工作,为什么?。我还想检查如何检查验证服务器和客户端,在我的代码中如果关闭js如果关闭[如果我的工作]来自将不会提交。并且php验证将无法工作。我也想知道我的编码架构有什么问题。经验brothes-&gt;请帮助我。

2 个答案:

答案 0 :(得分:0)

标签

<label name="errorMessage" id="ierrorMessage"><?php echo $errorMessage;?></label>

如果您在加载页面上没有错误,则不会创建,然后:

document.getElementById('ierrorMessage') /*is null*/

答案 1 :(得分:0)

使用jquery.Try像这样..

&#13;
&#13;
$("#login").on('click',function(){
username = $("#iusername").val();
password = $("#ipassword").val();
if(username=='' || password == ''){
console.log("You cant left username and password field empty.");
alert("You cant left username and password field empty.");
}

else{
$("#loginForm").submit();
}
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<center>
        <h2>LOGIN</h2>
        <form method="post" name="loginForm">
            <table cellpadding="4" cellspacing="4">
                <tr>
                    <td>USERNAME</td>
                    <td><input type="text" name="username" id="iusername" /></td>
                </tr>
                <tr>
                    <td>PASSWORD</td>
                    <td><input type="password" name="password" id="ipassword" /></td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td><input type="submit" id="login" name="submit" /></td>
                </tr>
            </table>
        </form>
        <br />
        </center>
&#13;
&#13;
&#13;