I am using CodeIgniter and jQuery validation. I have a registration form in that fields are Firstname, Lastname, email, mobile. Jquery validation is working perfectly. I am using the remote method to check the email id is existed or not in the database which also works. There is no issue in the form while registration.
Come to the update records
I am using the same jquery validation for both the form "Registration" and "Update". Even validation is also working but there is some issue in the remote method.
I haven't updated my email and then click on submit button it's showing "Email id already exist". AS per the process, this is the right but is there any other way, without update the email remote method should not call? or if the user updates the email field then remote method call for an update? or else should I check email before updating the record? or should I use separate validation for an update?
What is the best way to use? Any other idea?
Jquery Validation
$("#emp_register").validate({
// Specify the validation rules
rules: {
firstname:{
required: true,
minlength:3,
maxlength:50
},
lastname:{
required: true,
minlength:3,
maxlength:50
},
email: {
required: true,
email:true,
remote: {
url: baseUrl + "/Employee_control/checkemail_exist",
type: "post",
data: {
email: function () {
return $("#email").val();
}}
}
},
mobileno: {
required: true,
number:true,
minlength:10,
maxlength: 10
}
},
messages: {
email: {remote: "This email is already registered!"
}
},
submitHandler: function(form) {
form.submit();
}
});
Registration
public function employee_register(){
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
$this->form_validation->set_rules('firstname', 'Firstname', 'trim|required|min_length[3]|max_length[25]');
$this->form_validation->set_rules('lastname', 'Lastname', 'trim|required|min_length[3]|max_length[25]');
$this->form_validation->set_rules('email', 'Email', 'required');
$this->form_validation->set_rules('mobileno', 'Mobile no', 'trim|required|regex_match[/^[0-9]{10}$/]');
if ($this->form_validation->run() == FALSE)
{
$this->index();
}
else
{
$data = array(
'firstname'=>$this->input->post('firstname');,
'lastname'=>$this->input->post('lastname'),
'email_id'=>$this->input->post('email'),
'mobileno'=>$this->input->post('mobileno'),
);
$secure_data = $this->security->xss_clean($data);
$this->db->insert('tbl_employee',$secure_data);
redirect("Employee_control/index");
}
}
Update
public function update_employee_info(){
$update_key_id=$this->encryption->decrypt(base64_decode($this->input->get('key')));
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
$this->form_validation->set_rules('firstname', 'Firstname', 'trim|required|min_length[3]|max_length[25]');
$this->form_validation->set_rules('lastname', 'Lastname', 'trim|required|min_length[3]|max_length[25]');
$this->form_validation->set_rules('email', 'Email', 'required');
$this->form_validation->set_rules('mobileno', 'Mobile no', 'trim|required|regex_match[/^[0-9]{10}$/]');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('employee/employee_edit');
}
else
{
$updateData = array(
'firstname'=>$this->input->post('firstname'),
'lastname'=>$this->input->post('lastname'),
'email_id'=>$this->input->post('email'),
'mobileno'=>$this->input->post('mobileno')
);
$secure_updateData = $this->security->xss_clean($updateData);
$this->db->where('id', $update_key_id);
$this->db->update('tbl_employee', $secure_updateData);
redirect("Employee_control/employee_list");
}
}