javascript - Ajax表单提交总是抛出错误消息

标签 javascript php jquery ajax codeigniter

我有一个带验证功能的 JQuery ajax 注册表单。所有验证和表单提交都工作正常。但表单提交后,成功消息不起作用。它总是抛出错误消息。我正在使用 codeigniter。

我的jquery如下

<script>
    $(document).ready(function() {

        (function($) {
            "use strict";

            jQuery.validator.addMethod('answercheck', function(value, element) {
                return this.optional(element) || /^\bcat\b$/.test(value);
            }, "type the correct answer -_-");

            // validate contactForm form
            $(function() {
                $('#form_register').validate({
                    rules: {
                        email: {
                            required: true,
                            email: true
                        },
                        username: {
                            required: true,
                            minlength: 6
                        },
                        full_name: {
                            required: true,
                            minlength: 3
                        },
                        password: {
                            required: true,
                            minlength: 6
                        },
                        confirm_pass: {
                            required: true,
                            minlength: 6,
                            equalTo: "#password"
                        },
                        phone: {
                            required: true,
                            number: true,
                            minlength: 10
                        }
                    },
                    messages: {
                        email: {
                            required: "no email, no account"
                        },
                        username: {
                            required: "come on, you have to provide username",
                            minlength: "your username must consist of at least 6 characters"
                        },
                        full_name: {
                            required: "come on, you have a name don't you?",
                            minlength: "your name must consist of at least 3 characters"
                        },
                        password: {
                            required: "Please provide password!",
                            minlength: "thats all? really?"
                        },
                        confirm_pass: {
                            required: "Please confirm password!",
                            minlength: "thats all? really?",
                            equalTo: "Please enter the same password again."
                        },
                        phone: {
                            required: "come on, you have a phone don't you?",
                            number: "Phone number must contain digits only",
                            minlength: "your phone number must consist of at least 10 characters"
                        }
                    },
                    submitHandler: function(form) {
                        $('#loading').show();

                        $(form).ajaxSubmit({
                            type: "POST",
                            data: $(form).serialize(),
                            url: "<?php echo base_url(); ?>createcode",
                            success: function(data) {
                                $('#loading').hide();

                                $('#success').show();
                                $('#success').html(data);

                                if (data.indexOf('login now!') > 0) {
                                    $(form).trigger("reset");
                                }
                            },
                            error: function(req, status, err) {
                                console.log('something went wrong', status, err);
                                alert('something went wrong' + status + err);
                            }
                        });
                    }
                });
            });

        })(jQuery)
    });
</script>

我的 Controller (Createcode.php)如下

class Createcode extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('creates');
    }

    public function index() {
        if($this->input->post('register')) {
            $email = $this->input->post('email');
            $username = $this->input->post('username');
            $full_name = $this->input->post('full_name');
            $confirm_pass = $this->input->post('confirm_pass');
            $phone = $this->input->post('phone');

            $add = $this->creates->createAccount($email, $username, $full_name, $confirm_pass, $phone);

            if($add === TRUE) {
                echo "Your account created. You can login now!";
            } else if($add === FALSE) {
                echo 'Something went wrong. Tryagain later!';
            } else if($add == 'email') {
                echo 'Email already exists. Try another!';
            } else if($add == 'username') {
                echo 'Username already exists. Try another!';
            } else if($add == 'phone') {
                echo 'Mobile number already exists. Try another!';
            } else {
                echo 'Something went wrong. Tryagain later!';
            }
        }
    }
}

我的模型(creates.php)如下

class Creates extends CI_Model {

    public function createAccount($email, $username, $full_name, $confirm_pass, $phone) {
        $check_username = $this->db->query('SELECT * FROM tbl_account WHERE acc_username = "'.$username.'"');
        $check_phone = $this->db->query('SELECT * FROM tbl_account WHERE acc_phone = "'.$phone.'"');
        $check_email = $this->db->query('SELECT * FROM tbl_account WHERE acc_email = "'.$email.'"');

        if($check_email->num_rows() > 0) {
            return 'email';
        } else if($check_username->num_rows() > 0) {
            return 'username';
        } else if($check_phone->num_rows() > 0) {
            return 'phone';
        } else {
            $data = array(
                'acc_email' => $email,
                'acc_username' => $username,
                'acc_name' => $full_name,
                'acc_password' => $this->encrypt->encode($confirm_pass),
                'acc_phone' => $phone,
                'acc_created_on' => date("l, d/F/Y h:i:s A")
            );

            $add = $this->db->insert('tbl_account', $data);

            if($add == 1) {
                return TRUE;
            }
            else {
                return FALSE;
            }
        }
    }
}

上面的代码工作正常。并且数据库操作也正常。
成功提交表单后,将显示来自 error: 函数的出现错误消息。
如何修复此错误。我已经尝试了很多。有什么解决办法吗?

最佳答案

我怀疑主要问题就在这里

$(form).ajaxSubmit({...

我认为您需要在选择器中添加一些引号

$(`form`).ajaxSubmit({...

与您的问题无关,但我无法帮助自己...请考虑对您的模型函数进行重构

public function createAccount($email, $username, $full_name, $confirm_pass, $phone) 
{
//No point in making further queries if any given validation fails.
//So check the results after each query and return if it fails
    $check_username = $this->db->query('SELECT * FROM tbl_account WHERE acc_username = "'.$username.'"');
    if($check_email->num_rows() > 0) {
        return 'email';
    }

    $check_phone = $this->db->query('SELECT * FROM tbl_account WHERE acc_phone = "'.$phone.'"');
    if($check_username->num_rows() > 0) {
        return 'username';
    }

    $check_email = $this->db->query('SELECT * FROM tbl_account WHERE acc_email = "'.$email.'"');
    if($check_phone->num_rows() > 0) {
        return 'phone';
    }

    $data = array(
        'acc_email' => $email,
        'acc_username' => $username,
        'acc_name' => $full_name,
        'acc_password' => $this->encrypt->encode($confirm_pass),
        'acc_phone' => $phone,
        'acc_created_on' => date("l, d/F/Y h:i:s A")
    );
    //don't need all this
    //$add = $this->db->insert('tbl_account', $data);
        //if($add == 1) {
            //return TRUE;
        //}
        //else {
            //return FALSE;
        //}

    //when this does exactly the same thing
    return $this->db->insert('tbl_account', $data);
}

还要考虑 Createcode::index 中的这组条件

if($add === TRUE){
    echo "Your account created. You can login now!";
} else if($add == 'email'){
    echo 'Email already exists. Try another!';
} else if($add == 'username'){
    echo 'Username already exists. Try another!';
} else if($add == 'phone'){
    echo 'Mobile number already exists. Try another!';
} else {
    echo 'Something went wrong. Tryagain later!';
}

它的运行速度与您现有的一样快,甚至可能更快,因为它消除了一种条件评估。

关于javascript - Ajax表单提交总是抛出错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39288797/

相关文章:

php - 使用 valgrind 调试 php 扩展的内存泄漏

php - PHP Comet 聊天系统的可扩展性问题

jquery - jQuery 数据表中的列排序

javascript - PHP ajax不显示数据库中的任何数据

javascript - redux-saga select() 返回未定义

javascript - 对象不支持属性或方法 'data'

javascript - 动态隐藏 ExtJS Fieldset 中的控件

php - 将用户电子邮件与来自 IPN 的 PayPal 电子邮件匹配

javascript - 如何在循环中实现Jquery复选框全选/取消全选功能?

javascript - 为什么 lang sass 在 .vue 样式标签中不起作用?