Codeigniter Re-populate form in JQuery Colorbox when serverside validation fails -
i creating basic crud. works fine except form repopulation. here detail doing.
i displaying list of employees. there links add together new. , edit , delete each employee. on add together new or edit populate jquery colorbox , load form in it. here link hits controller method.
<a href="<?php echo site_url('employees/form/'.$employee['id']);?>" id="popupeditformtrigger" return="<?php echo site_url('employees/entries'); ?>" >add new</a>
as can see here hitting controller method load form. works fine. in color box form.js here code.
var datastring = $('#updateform').serialize(); var url = $('#updateform').attr('url'); $.ajax({ type: "post", url: url, data: datastring, success: function() { $.fn.colorbox.close(); } }); homecoming false;
it closes color box when ajax successsfull. homecoming false prevents list view go farther , then
return="<?php echo site_url('employees/entries'); ?>"
is run , refreshes entries in table.
controller
public function submit() { $this->load->library('form_validation'); $this->form_validation->set_rules('name',' employee name'.'trim|required'); $this->form_validation->set_rules('salary',' salary'.'trim|required'); $this->form_validation->set_rules('description',' description'.'trim|required'); if($this->form_validation->run()){ $data['name'] = $post['name']; $data['salary'] = $post['salary']; $data['description'] = $post['description']; $result = $this->employeesmodel->insertemployee($data); }else{ $this->load->view('employee_form'); } }
problem :
the problem want repopulate form in colorbox can display user validation errors. colorbox closes whether insertion performed or validation fails. want if validation fails populate form 1 time again in colorbox , prevent hyper link attribute homecoming go site_url('employees/entries'). how can that? sure jquery can handle don't know how. suggestions required.
okay, concept making here what's causing problem. want close form if there no validation errors. using success function not mean that. success function
tells whether ajax request
has been made correctly or not regardless output php
. need is...
on php side:
if(ok){ $data['res'] = '1'; // means ok } else{ $data['res'] = '0'; // means had problem validation. } // pass info in json format or other format need.
the above code sample clarify idea.
then what's important.. on jquery side:
success: function(data) { if(data.res == '1') { $.fn.colorbox.close(); } else { // display errors } }
so success function tells ajax request
has been made successfully. within can check if validation ok or not.
jquery codeigniter popup
No comments:
Post a Comment