2015-08-14 78 views
0

如何從不使用CodeIgniter表單創建的表單驗證輸入數據?CodeIgniter中HTML表單數據的表單驗證

我的形式如下:

<form method="POST" name="frmNewUser" action="<?php echo site_url('users/signup'); ?>" id="contact"> 
 
\t \t <div class="lable"> 
 
\t \t   <div class="col_1_of_2 span_1_of_2"><input name="u_fname" type="text" class="text" value="First Name" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'First Name';}" id="active"></div> 
 
       <div class="col_1_of_2 span_1_of_2"><input name="u_lname" type="text" class="text" value="Last Name" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Last Name';}"></div> 
 
       <div class="clear"> </div> 
 
\t \t </div> 
 
\t \t <div class="lable-2"> 
 
\t \t   <input name="u_email" type="text" class="text" value="[email protected] " onfocus="this.value = '';" onblur="if (this.value == '') {this.value = '[email protected] ';}"> 
 
\t \t   <input name="u_pwd" type="password" class="text" value="Password " onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Password ';}"> 
 
\t \t </div> 
 
\t \t <h3>By creating an account, you agree to our <span class="term"><a href="#">Terms & Conditions</a></span></h3> 
 
\t \t <div class="submit"> 
 
\t \t \t <input type="submit" name="submit" id="submit" value="Submit" > 
 
\t \t </div> 
 
\t \t <div class="clear"> </div> 
 
\t \t </form>

這裏是我的控制器:

function signup(){ 
     $this->load->library('form_validation'); 

     $u_fname = $this->input->post('u_fname'); 
     $u_lname = $this->input->post('u_lname'); 
     $u_email = $this->input->post('u_email'); 
     $u_pwd = $this->input->post('u_pwd'); 

     $this->form_validation->set_rules($this->input->post('u_fname'), 'First Name', 'required'); 

     if($this->form_validation->run() == FALSE){ 
      echo "error"; 
     } 
    } 

然而的run()form_validation的函數返回即使u_fname有假輸入值。 如何使用post方法從表單中獲取數據並使用CodeIgniter中的form_validation驗證數據?

謝謝

回答

0

問題解決了。 我只需要改變

$this->form_validation->set_rules($this->input->post('u_fname'), 'First Name', 'required'); 

$this->form_validation->set_rules('u_fname', 'First Name', 'required'); 

前一個值傳遞給函數的同時,新通過的對象的「名稱」。