开发者

Problem with ajax when processing a form using codeigniter

I'm having a problem when trying to submit a form via ajax. If i enter just the first name and submit it, it gives me the error on the last name. If i enter just the last name, it gives me the error on the first name. This is what I expect.

Now if i enter both a first name and last name and submit the form, in firebug it gives me the error "msg is null" in the console. How can msg be null when both first and last name are submitted, but not when first or last name are submitted separately?

My form contains these:

<p class="inline-medium-label"><span class="relative">
   <?php echo form_error('firstName'); ?>
   <label for="firstName" class="required">First Name</label>
   <input type="text" class="full-width" name="firstName" value="<?php echo set_value('firstName'); ?>" /><br />
</span><开发者_运维百科/p>

<p class="inline-medium-label"><span class="relative"> 
   <?php echo form_error('lastName'); ?>
   <label for="lastName" class="required">Last Name</label>
   <input type="text" class="full-width" name="lastName" value="<?php echo set_value('lastName'); ?>" /><br />
</span></p>

My ajax call

$.ajax({
   url: 'addMember',
   type: 'post',
   data: data,
   dataType: 'json',
   success: function(msg) {
      if(msg.validate === false) {  // if there are validation errors...
         if(msg.firstName != '') {
        $('input[name="firstName"]').addClass('error');
        $('input[name="firstName"]').after('<span class="check-error"></span>');
     }
     if(msg.lastName != '') {
        $('input[name="lastName"]').addClass('error');
        $('input[name="lastName"]').after('<span class="check-error"></span>');
     }
      } else {
         $('div.modal-content').append('<h2>User Added Successfully!</h2>');
      }
   }
});

My controller that handles the ajax

function addMember ()
{   
   $data['sess'] = $this->session;  

   // validates and sanitizes the user's input
   $this->form_validation->set_rules('firstName', 'First Name', 'trim|required|max_length[30]|xss_clean');
   $this->form_validation->set_rules('lastName', 'Last Name', 'trim|required|max_length[30]|xss_clean');

   // if there are validation errors...
   if($this->form_validation->run() == FALSE)
   {    
      if($this->input->post('ajax'))    // if the form was submitted with ajax...
      {
         echo json_encode(array('validate'=>FALSE, 
                            'firstName'=>form_error('firstName'), 
                'lastName'=>form_error('lastName')
                  ));
      }
      else  // else if NOT submitted with ajax...
      {
         echo 'Ajax not used';
      }
   }
   else // if there are NO validation errors
   {

   }    
}


Well it's simply because in your else clause (when the data validates) you don't do anything.

So yes, the data returned to your javascript function will be null

   else // if there are NO validation errors
   {
        //You should output something here
   }  

Hope that helps

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜