2014-01-13 52 views
0

我正在使用codeigniter。以下是我的註冊頁面的代碼。

控制器保留用戶輸入的數據

function register() 
    { 
     $this->load->library('form_validation'); 
     $this->form_validation->set_rules('first_name', 'First Name',  'trim|required'); 
      $this->form_validation->set_rules('last_name', 'Last Name', 'trim|required'); 
      $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]|is_unique[users.username]'); 
      $this->form_validation->set_rules('user_email', 'Email', 'trim|required|valid_email|is_unique[users.user_email]'); 
      $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]'); 
      $this->form_validation->set_rules('ans_1', 'Security answer 1', 'required');  
       $this->form_validation->set_rules('ans_2', 'Security answer 2',  'required'); 
      if($this->form_validation->run() == FALSE) { 
      //not validated - reload the view and display errors 
       $this->load->database(); 

      $this->load->model('user'); 
      $data['ques'] = $this->user->get_que(); 
      $this->load->view('register',$data); 

      } 
      else { 
      $this->load->database(); 

      $this->load->model('user'); 
      //create user 
      $this->user->create_user(); 
      $this->thank(); 
      } 
    } 



查看

<!DOCTYPE html> 
<html> 
<head> 
    <title> Login</title> 
    <link rel="stylesheet" href="http://localhost/cinema/assets/css/form.css"> 

</head> 
<body> 




      <form action="http://localhost/cinema/login/register" method="post" accept-charset="utf-8" class="username register" > 
      <h3>User Registration form:</h3> 
      <p><label for="first_name">First Name</label> 
       <input type="text" name="first_name" /></p> 
      <p><label for="last_name">Last Name</label> 
       <input type="text" name="last_name" /></p> 

      <p><label for="username">Username</label> 
       <input type="text" name="username" /></p> 

      <p><label for="user_email">Email</label> 
       <input type="text" name="user_email" /></p> 

    <p><label for="password">Password</label> 
      <input type="password" name="password" /></p> 
     <br><font size='4'>Select security question 1:</font> 
      <select name="que_1"> 
     <?php 
      foreach($ques as $que) 
     { 
      echo '<option value="'.$que->que_ID.'">'.$que->que.'</option>'; 
     } 
     ?> 
     </select><br><br> 
     <p><label for="ans_1">Answer:</label> 
       <input type="text" name="ans_1" /></p> 
     <br><font size='4'>Select security question 2:</font> 
      <select name="que_2"> 
     <?php 
      foreach($ques as $que) 
     { 
      echo '<option value="'.$que->que_ID.'">'.$que->que.'</option>'; 
     } 
     ?> 
     </select><br><br> 
     <p><label for="ans_2">Answer:</label> 
       <input type="text" name="ans_2" /></p> 

     <input type="submit" name="btnSubmit" class="styled-button-8" style="float:right; margin-right:300px; margin-top:-10px;" value="Sign Up!" 
      /> 
      <font color="red" size="4"><b> 
     <?php echo validation_errors(); ?></b></font> 
      </form> 
</body> 
</html> 

    <br> 

我的問題是,如果用戶填寫無效數據並點擊註冊按鈕,頁面刷新,並顯示驗證錯誤,但用戶輸入的數據不會保留在那裏。他必須再次填寫整個表格。我希望在這種情況下保留並顯示用戶數據。在此先感謝.. :)

回答

0

爲此,您需要set_value()

對於輸入的Fileds

<input type="text" name="last_name" value='<?php echo set_value('last_name')?>'/> 

這將從$ _POST獲取值,並在驗證失敗後進行打印。也將第二個參數作爲默認值。

<?php echo set_value('last_name','John')?> 

閱讀Form Helper類。

+0

非常感謝!有效。選擇什麼選項?他們再次刷新。 – user3142334

0

你可以嘗試這樣。您可以維護會話以在驗證檢查時顯示用戶字段數據。我在表單中添加了一個Sample會話。

您的看法:

<?php session_start(); ?> 
<form action="http://localhost/cinema/login/register" method="post" accept-charset="utf-8" class="username register" > 
    <h3>User Registration form:</h3> 
    <p><label for="first_name">First Name</label> 
     <input type="text" name="first_name" value="<?php echo $_SESSION['first_name']; ?>" /></p> 
    <font color="red" size="4"><b> 
     <?php echo validation_errors(); ?></b></font> 
</form> 

你的控制器:

function register() { 
    $this->load->library('form_validation'); 
    $firstName = $this->form_validation->set_rules('first_name', 'First Name', 'trim|required'); 
    $this->session->set_userdata('first_name', $firstName); 
    if ($this->form_validation->run() == FALSE) { 
     //redirect to register page 
    } else { 
     //register user details 
     $this->session->unset_userdata('first_name'); 
    } 
0

更好的解決辦法是:使用後數據

<input type="text" name="last_name" value='<?php echo $this->input->post('last_name')?>'/> 

當您使用SET_VALUE()時,必須發送場即使在使用時不需要驗證。 但是對於Post,你不想做任何額外的工作。

0

在您的控制器中放入一個數據數組並將用戶輸入的值傳遞給那個。然後如果存在表單驗證錯誤,則將這些數據傳遞給視圖。

在視圖中使用CI set_value()方法顯示先前輸入的值。