1

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
   {

   }    
}

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

1 Comment

I knew it was something easy! Thanks for your help, i've spent hours on this problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.