在Ajax表单上返回false不起作用

时间:2013-07-21 02:23:40

标签: php javascript ajax json codeigniter

我有一个表单,用于评估我的流程页面上数据库中的信息,并在必要时返回错误。我正在使用Ajax所以它实际上不应该进入进程页面并加载我在Json中编码的内容以返回。这是我的表格加上Javascript:

<form method="post" action="../user/process_login" id="login_form">
    <input type="hidden" name="action" value="login">
    <label>Email Address:</label>
    <input type="text" id="email" name="email" placeholder="Email" />       
    <label>Password:</label>
    <input type="password" id="password1" name="password0" placeholder="Password" />
    <input type="submit" id="submitbtn" placeholder="Submit" class="button"/>
</form>

Javascript:

<script type="text/javascript">
$(document).ready(function(){
    $('#login_form').submit(function(){
        $.post
        (
            $(this).attr('action'),
            $(this).serialize(),
            function(data){
                if (data['errors'] == '') {
                    consle.log(data);
                };
                else{
                    console.log(data);
                    $('#alert_box').html(data);
                };
            },
            "json"
        );
        return false;
    });
});
</script>

以下是我的验证码的相关部分:

if (count($user) > 0 AND $decrypted_password == $this->input->post('password0')) 
    {
        $this->session->set_userdata('user_session', $user);
        $this->load->view('main.php');
    }
    else
    {
        $errors = "<div class='alert-box alert' id='error-box'><p>Your login information did not match our reccords. Try again</p></div>";
    echo json_encode($errors);
    }   

2 个答案:

答案 0 :(得分:0)

我认为这是因为您的代码中存在错误。我在下面指出

这里没有错误----&gt; WITHOUT ERRORS

这是错误-----&gt; WITH ERRORS

正如您所看到的,返回false确实可以正常工作。

<script type="text/javascript">
$(document).ready(function(){
$('#login_form').submit(function(){
    $.post
    (
        $(this).attr('action'),
        $(this).serialize(),
        function(data){
            if (data['errors'] == '') {
                consle.log(data);
            };   <----------------------- Error Not supposed to be there
            else{
                console.log(data);
                $('#alert_box').html(data);
            };   <----------------------- I think will still work but don't need
        },
        "json"
    );
    return false;
});
});
</script>

答案 1 :(得分:0)

您不能使用输入类型提交,在调用Ajax函数时,如果您使用它,您的代码将无法按预期工作,因此将其更改为:

<input type="button" id="submitbtn" placeholder="Submit" class="button"/>
相关问题