我有一个带验证的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:
消息显示
如何解决此错误。我已经尝试了很多。有没有解决方案?
答案 0 :(得分:0)
我怀疑主要问题在这里
$(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!';
}
它将以你所拥有的速度运行,或者更快,因为它消除了一个条件评估。