2011-06-23 170 views
0

我想驗證表單並將其保存在我的數據庫中。我使用codeigniter的驗證庫,它失敗。PHP codeigniter表單驗證與AJAX失敗

本所認爲

function save_form(){ 
    $.ajax({ 
     url: '/projects/cafe/index.php/welcome/save_form', 
     type: 'post', 
     data: {'fname' : $('#fname').val(), 
       'lname' : $('#lname').val(), 
       'story_name' : $('#story_name').val(), 
       'email' : $('#email').val(), 
       'address_street' : $('#address_street').val(), 
       'city' : $('#city').val(), 
       'state' : $('#state').val(), 
       'zip' : $('#zip').val(), 
       'phone1' : $('#phone1').val(), 
       'phone2' : $('#phone2').val(), 
       'phone3' : $('#phone3').val(), 
       'age_18' : $('#age_18').val(), 
       'ci_csrf_token' : $('input[name=ci_csrf_token]').val()}, 
     success: function(data, textStatus, jqXHR){ 
      //Redirect user to next page here... 
      if(data == '1'){ 
       location.href = 'http://www.voltage.com/projects/cafe/index.php/welcome/create4'; 
      }else{ 
       alert('form save failed'); 
      } 
     }, 
     error: function(jqXHR, textStatus, errorThrown){ 
      console.log('error: '+jqXHR); 
      console.log(textStatus); 
      console.log(errorThrown); 
     } 
    }); 
} 

裏面的AJAX功能,這是在我的控制器功能

class Welcome extends MY_Controller { 
public function save_form(){ 
     $this->load->helper(array('form', 'url')); 
     $this->load->library('form_validation'); 

     $this->form_validation->set_rules('fname', 'First Name', 'required|min_length[6]|max_length[12]'); 
     $this->form_validation->set_rules('lname', 'Last Name', 'required'); 
     $this->form_validation->set_rules('story_name', 'Story Name', 'required'); 
     $this->form_validation->set_rules('email', 'email', 'required'); 
     $this->form_validation->set_rules('address_street', 'Street Address', 'required'); 
     $this->form_validation->set_rules('city', 'City', 'required'); 
     $this->form_validation->set_rules('state', 'State', 'required'); 
     $this->form_validation->set_rules('zip', 'Zip', 'required'); 
     $this->form_validation->set_rules('phone1', 'Area Code', 'required'); 
     $this->form_validation->set_rules('phone2', 'Phone Number', 'required'); 
     $this->form_validation->set_rules('phone3', 'Phone Number', 'required'); 
     $this->form_validation->set_rules('age_18', 'Age Verification', 'required'); 

     if($this->form_validation->run() == FALSE) 
      die(); 

     $this->load->model('user'); 

     $results = $this->user->save_form($this->session->userdata('fbid'), 
              $this->input->post()); 

     echo $results; 
    } 

我感到困惑的是如何if($this->form_validation->run() == FALSE)傳球,如果我沒有任何phone1,phone2或phone3的值,這是必需的。

 $this->form_validation->set_rules('phone1', 'Area Code', 'required'); 
     $this->form_validation->set_rules('phone2', 'Phone Number', 'required'); 
     $this->form_validation->set_rules('phone3', 'Phone Number', 'required'); 

這就是被髮送到服務器fname=First&lname=Last&story_name=Your+Story&email=yourname%40domain.com&address_street=Street&city=City&state=State&zip=Zip&phone1=&phone2=&phone3=&age_18=

+0

什麼是PHP的輸出它運行時的腳本?它是否將表單保存在數據庫中並立即返回1? – tgriesser

回答

0

我在這個一直盯着,不能明白爲什麼它是行不通的。當其他字段被忽略或者嚴格限制在這三個字段時,您是否也會收到意想不到的行爲?唯一「奇數」我明白了,也許這是從正確加載保持form_validation庫,是

<?=fname?> 

你逝去的這行:

$this->form_validation->set_rules('<?=$fname?>', 'First Name', 'required|min_length[6]|max_length[12]'); 
+1

我改變了它,是一個錯誤它的fname不是驗證通過,如果我省略所有字段 –