i have jquery ajax register form validation. validation , form submission working fine. after form submission message in success not working. throws error message. using codeigniter.
my jquery below
<script> $(document).ready(function() { (function($) { "use strict"; jquery.validator.addmethod('answercheck', function(value, element) { return this.optional(element) || /^\bcat\b$/.test(value); }, "type 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, have provide username", minlength: "your username must consist of @ least 6 characters" }, full_name: { required: "come on, have name don't you?", minlength: "your name must consist of @ 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 same password again." }, phone: { required: "come on, have phone don't you?", number: "phone number must contain digits only", minlength: "your phone number must consist of @ 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>
my controller (createcode.php) below
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. can login now!"; } else if($add === false) { echo 'something went wrong. tryagain later!'; } else if($add == 'email') { echo 'email exists. try another!'; } else if($add == 'username') { echo 'username exists. try another!'; } else if($add == 'phone') { echo 'mobile number exists. try another!'; } else { echo 'something went wrong. tryagain later!'; } } } }
my model (creates.php) below
class creates extends ci_model { public function createaccount($email, $username, $full_name, $confirm_pass, $phone) { $check_username = $this->db->query('select * tbl_account acc_username = "'.$username.'"'); $check_phone = $this->db->query('select * tbl_account acc_phone = "'.$phone.'"'); $check_email = $this->db->query('select * tbl_account 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; } } } }
the above code working fine. , database operation working.
after success form submission something went wrong
message error:
function displaying.
how fix error. have tried lot. there solution?
i suspect main problem here
$(form).ajaxsubmit({...
i think need quotes in selector
$(`form`).ajaxsubmit({...
not related problem cannot myself... please consider refactor of model function
public function createaccount($email, $username, $full_name, $confirm_pass, $phone) { //no point in making further queries if given validation fails. //so check results after each query , return if fails $check_username = $this->db->query('select * tbl_account acc_username = "'.$username.'"'); if($check_email->num_rows() > 0) { return 'email'; } $check_phone = $this->db->query('select * tbl_account acc_phone = "'.$phone.'"'); if($check_username->num_rows() > 0) { return 'username'; } $check_email = $this->db->query('select * tbl_account 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 //$add = $this->db->insert('tbl_account', $data); //if($add == 1) { //return true; //} //else { //return false; //} //when same thing return $this->db->insert('tbl_account', $data); }
also consider set of conditionals in createcode::index
if($add === true){ echo "your account created. can login now!"; } else if($add == 'email'){ echo 'email exists. try another!'; } else if($add == 'username'){ echo 'username exists. try another!'; } else if($add == 'phone'){ echo 'mobile number exists. try another!'; } else { echo 'something went wrong. tryagain later!'; }
it run fast 1 have or maybe faster eliminates 1 condition evaluation.
Comments
Post a Comment