2014-11-05 80 views
0

嘗試閱讀谷歌和堆棧流中的所有網頁和文檔,但仍無法解決問題。Codelgniter表單驗證在驗證後顯示雙重重複表單

我試圖做一個簡單的數據驗證註冊表單,它變成顯示另一個表單之後,我按下提交顯示錯誤消息與一個新的表單。

我是這種語言的新手,所以請讓我知道如果我沒有足夠的代碼或信息。

控制器帳戶:

<?php 
class Account extends MY_Controller { 
    public function __construct() { 
     parent::__construct(); 
     session_start(); 
     $this->load->model('user'); 
     $this->load->helper(array('form','url','html')); 
     $this->load->library('session'); 
     $this->load->library('form_validation'); 

    } 

    public function registration() { 
     $data = $this->user->users_info(); 
     $this->load->view('account/registration',$data); 
     $this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[20]'); 
     $this->form_validation->set_rules('email', 'Email', 'required|valid_email'); 
     $this->form_validation->set_rules('password', 'Password ', 'required|matches[passconf]|min_length[5]'); 
     $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required'); 

     if($this->input->post('submit')) { 
      $username= $this->input->post('username'); 
      $email= $this->input->post('email'); 
      $query_u= $this->user->retrieve_by_username($username); 
      $query_e= $this->user->retrieve_by_email($email); 
      if ($this->form_validation->run() == FALSE){ 
       $this->load->view('account/registration',$data); ←---------------- (I think this is wrong, it makes load the second form out.) 
      } 
      else{ 
       if(!empty($query_u) or !empty($query_e)) { 
        redirect('account/registrat'); 
       } 
       else { 
        $data = array(
        'username'=>$this->input->post('username'), 
        'email'=>$this->input->post('email'), 
        'password'=>$this->input->post('password'), 
        'is_admin'=>0, 
        ); 
        $this->user->create_user($data); 
        redirect('/account/login'); 
       } 
      } 
     } 
    } 

查看和registration.php

<center> 
<?php echo form_open_multipart('account/registration'); ?> 
<h5><?php echo $b_username;?> &nbsp;(Minimum 5 characters)</h5> 
<input type="text" name="username" id="username" value="<?php echo set_value('username'); ?>" size="50" /><?php echo form_error('username'); ?> 
<h5><?php echo $b_email;?></h5> 
<input type="text" name="email" value="<?php echo set_value('email'); ?>" size="50" /> 
<?php echo form_error('email'); ?> 
<h5><?php echo $b_password;?> &nbsp;(Minimum 5 characters)</h5> 
<input type="text" name="password" value="<?php echo set_value('password'); ?>" size="50" /> 
<?php echo form_error('password'); ?> 
<h5><?php echo $b_passconf;?></h5> 
<input type="text" name="passconf" value="" size="50" /> 
<?php echo form_error('passconf'); ?> 
<h5></h5> 
<div><?php echo form_submit('submit', 'Submit') ?></div> 
</center> 

型號user.php的

<?php 
class User extends CI_Model { 
    function __construct() { 
     parent::__construct(); 
     $this->load->database(); 
    } 
    function users_info() { 
     $data['b_id'] = 'id'; 
     $data['b_username'] = 'Username'; 
     $data['b_email'] = 'Email'; 
     $data['b_password'] = 'Password'; 
     $data['b_passconf'] = 'Enter Password Again'; 
     $data['b_is_admin'] = 'Is_admin'; 
     $data['b_default_privacy'] = 'Default_privacy'; 
     $data['b_first_name'] = 'First_Name'; 
     $data['b_last_name'] = 'Last_Name'; 
     $data['b_gender'] = 'Gender'; 
     $data['b_birthday'] = 'Birthday'; 
     $data['b_friend_id'] = 'Friend_id'; 
     $data['b_weight'] = 'Weight'; 
     $data['b_height'] = 'Height'; 
     $data['b_daily_cal_intake'] = 'Daily_calorie_intake'; 
     $data['b_target_weight'] = 'Target_weight'; 
     $data['b_regional_id'] = 'Region'; 
     $data['b_profile_pic'] = 'Profile Picture'; 
     return $data; 
    } 
    function retrieve_by_username($username) { 
     $query = $this->db->get_where('001_users',array('username'=>$username)); 
     return $query->row_array(); 
    } 
    function retrieve_by_email($email) { 
     $query = $this->db->get_where('001_users', array('email'=>$email)); 
     return $query->row_array(); 
    } 

回答

0

你的職能轉變到這一點,並嘗試..

public function registration() 
{ 

    $this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[20]'); 
    $this->form_validation->set_rules('email', 'Email', 'required|valid_email'); 
    $this->form_validation->set_rules('password', 'Password ', 'required|matches[passconf]|min_length[5]'); 
    $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required'); 

    if ($this->form_validation->run() == false) // if validation fails for first time and every time when ever condtion is not satisified 
    { 
     $data = $this->user->users_info(); 
     $this->load->view('account/registration',$data); 
    } 
    else 
    { 
     $username= $this->input->post('username'); 
     $email= $this->input->post('email'); 
     $query_u= $this->user->retrieve_by_username($username); 
     $query_e= $this->user->retrieve_by_email($email); 

     if(!empty($query_u) or !empty($query_e)) { 
      redirect('account/registrat'); 
     } 
     else { 
      $data = array(
       'username'=>$this->input->post('username'), 
       'email'=>$this->input->post('email'), 
       'password'=>$this->input->post('password'), 
       'is_admin'=>0 
      ); 
      $this->user->create_user($data); 
      // send sucess msg here 
      redirect('/account/login'); 
     } 

    } 
} 
+0

我有點明白,要加載帶有「\t \t \t如果該視圖($這個 - > form_validation->的run()== FALSE)你的邏輯{ \t \t \t \t $這個 - >負載>視圖( '賬戶/註冊',$數據); \t \t \t}'但我在哪裏放'$ this-> input-> post('submit')'? – Adrianktng 2014-11-06 00:20:50

+0

直到並且除非你有兩個或兩個以上的提交按鈕(更新/插入等),並且表單指向相同的函數,否則不需要它。這裏有一個只有一個功能的按鈕。所以在這裏沒有使用提交按鈕。 – 2014-11-06 05:13:10

+0

好的,非常感謝你。現在瞭解更多。 – Adrianktng 2014-11-06 06:54:38