我正在使用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>
我的問題是,如果用戶填寫無效數據並點擊註冊按鈕,頁面刷新,並顯示驗證錯誤,但用戶輸入的數據不會保留在那裏。他必須再次填寫整個表格。我希望在這種情況下保留並顯示用戶數據。在此先感謝.. :)
非常感謝!有效。選擇什麼選項?他們再次刷新。 – user3142334