2011-06-23 18 views
0

フォームを検証してデータベースに保存しようとしています。私はcodeigniterの検証ライブラリを使用していますが、失敗します。php codeigniter form validation with ajax failed

これはこれは私が混乱している何

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)を渡す方法である私のコントローラ内の関数である

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機能であります必要な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

:唯一の事は「奇数」は、私が見て、多分これが正しく読み込まからform_validationライブラリーを保っている、あなたはこのラインで渡している

<?=fname?> 

です実行時のスクリプト?それはデータベースにフォームを保存し、今すぐ1を返しますか? – tgriesser

答えて

0

に送信されるものです。他のフィールドが省略されている場合や、これら3つのフィールドに厳密に限定されている場合にも、予期しない動作をしていますか? PHPの出力が何であるかを

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

私はそれを変更しました、間違いでしたfnameではありませんすべてのフィールドを省略した場合 –