使用codeigniter处理表单时出现的ajax问题

时间:2021-07-06 19:39:26

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.

我在尝试通过ajax提交表单时遇到了一个问题。如果我只输入第一个名字并提交它,它会给我最后一个名字的错误。如果我只输入姓氏,它会给我第一个名字的错误。这就是我所期望的。

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?

现在,如果我同时输入姓名和姓氏并提交表单,在firebug中,它会在控制台显示错误“msg为空”。如果同时提交了姓和名,而不是分别提交了名和名,msg如何为空?

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调用

$.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

处理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 个解决方案

#1


4  

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

因为在else子句中(当数据验证时)你什么都不做。

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

因此,返回到javascript函数的数据将为空

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

Hope that helps

希望这有助于

#1


4  

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

因为在else子句中(当数据验证时)你什么都不做。

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

因此,返回到javascript函数的数据将为空

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

Hope that helps

希望这有助于