【问题标题】:Return false on Ajax form not working在 Ajax 表单不工作时返回 false
【发布时间】:2013-07-21 02:23:40
【问题描述】:

我有一个表单,可以在我的流程页面上评估数据库中的信息,并在必要时返回错误。我正在使用 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);
    }   

【问题讨论】:

  • 不要以纯文本形式存储密码。
  • 您在控制台中看到了什么?
  • $decrypted_password 不可可解密。
  • 密码不是明文存储的。那只是我用来比较存储密码的变量。现在我想到了,我将通过加密输入并将其与存储的密码进行比较来反转它。
  • @JDillon522:不要加密它,散列它。根本不可能解密密码。

标签: php javascript ajax json codeigniter


【解决方案1】:

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

这里没有错误---->WITHOUT ERRORS

这是错误 -----> WITH ERRORS

如您所见,return 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>

【讨论】:

  • @JDillon522 您的意思是代码仍在将您定向到该页面?页面上有 jquery script 标签吗?
  • @JDillon522 我在顶部添加了另一个小提琴作为示例,以表明 return false 有效。或e.preventDefault();。我测试了两者。但是,如果您有更多代码,则可能会出现另一个错误。
【解决方案2】:

不能使用输入类型提交,在调用Ajax函数时,如果使用了,你的代码将无法按预期工作,所以改成:

<input type="button" id="submitbtn" placeholder="Submit" class="button"/>

【讨论】:

    猜你喜欢
    • 2012-11-11
    • 1970-01-01
    • 2016-12-01
    • 2014-11-14
    • 2020-11-02
    • 2012-01-27
    • 1970-01-01
    • 1970-01-01
    • 2017-05-13
    相关资源
    最近更新 更多