簡體   English   中英

Ajax表單提交總是拋出錯誤消息

[英]Ajax form submit always throw error message

我有一個帶驗證的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>

我的控制器(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;
            }
        }
    }
}

上面的代碼工作正常。 數據庫操作也正常。
成功表單提交something went wrong消息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!';
}

它將以您擁有的速度運行,或者更快,因為它消除了一個條件評估。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM