我已经遵循了CodeIgniter的学习教程,我目前正在创建一个程序,在我的参考资料(链接张贴在下面)上使用相同的程序,其中有一个登录表格。函数没有问题,我只想将validation_errors()放在textfield的右侧,如果用户名为空或不正确,错误消息将显示在textfield的右侧。有办法这样做吗?或者我应该使用javascript而不是使用这个validation_errors()?希望有人能帮我。谢谢!
下面是查看的代码:
<h3>Log-In with CodeIgniter</h3>
<?php echo validation_errors(); ?>
<?php echo form_open('verifylogin'); ?>
<label for="username">Username:</label>
<input type="text" size="20" id="username" name="username"/>
<br/>
<label for="password">Password:</label>
<input type="password" size="20" id="password" name="password"/>
<br/>
<input type="submit" value="Log-In"/>
</form>下面是控制器中的代码:
function index() {
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database');
if($this->form_validation->run()==FALSE) {
$this->load->view('login_view');
}
else {
redirect('home', 'refresh');
}
}
function check_database($password) {
$username=$this->input->post('username');
$result=$this->user->login($username, $password);
if($result) {
$sess_array=array();
foreach($result as $row) {
$sess_array=array('id'=>$row->id, 'username'=>$row->username);
$this->session->set_userdata('logged_in', $sess_array);
}
return TRUE;
}
else {
$this->form_validation->set_message('check_database', 'Invalid username or password');
return FALSE;
}
}参考: http://www.codefactorycr.com/login-with-codeigniter-php.html




发布于 2014-07-23 07:08:47
<h3>Log-In with CodeIgniter</h3>
<?php if (form_error('password')=="Invalid username or password")
{
echo form_error('password');
}
?>
<?php echo form_open('verifylogin'); ?>
<label for="username">Username:</label>
<input type="text" size="20" id="username" name="username"/><?php echo form_error('username'); ?>
<br/>
<label for="password">Password:</label>
<input type="password" size="20" id="password" name="password"/><?php echo form_error('password'); ?>
<br/>
<input type="submit" value="Log-In"/>
</form>发布于 2014-07-23 07:46:36
<?php echo form_open('verifylogin'); ?>这将在一次显示所有形式的错误-它们以批量错误的形式出现(而不是分裂的)。
<?php echo form_error('username'); ?>
<?php echo form_error('password'); ?>这是显示单个错误的正确方法,其中CodeIgniter为您提供了显示错误的方法。
并搜索显示错误的()
您可以在输入字段的span标记中显示它们,以获得所需的O/P类型。
发布于 2014-07-25 01:58:06
我就是这么做的..。
$this->form_validation->set_message('check_database', '<br/><br/>Invalid username or password');我将<br/>添加到无效的用户名或密码中,这样它就不会放置到密码字段。
https://stackoverflow.com/questions/24903793
复制相似问题