所以我的注册表格运作良好。但是当尝试单击提交按钮而没有任何输入时,页面只是重新加载而没有任何应该出现的警告。这是我的控制器:
<?php
class Signup extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper(array('form','url'));
$this->load->library(array('session','form_validation'));
$this->load->database();
$this->load->model('user_model');
}
function index()
{
//set form validation rules
$this->form_validation->set_rules('fname','Full Name', 'trim|required|alpha|min_length[3]|max_length[30]|xss_clean');
$this->form_validation->set_rules('username','Last Name', 'trim|required|alpha|min_length[3]|max_length[30]|xss_clean');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|is_unique[user.user_email]');
$this->form_validation->set_rules('password','Password','trim|required|md5');
//submit
if($this->form_validation->run() == FALSE)
{ //FAILS
$this->load->library('../controllers/page');
$this->page->register();
} else
{
//insert user details into db
$data = array(
'user_nama' => $this->input->post('fname'),
'username' => $this->input->post('username'),
'user_email' => $this->input->post('email'),
'user_phone' => $this->input->post('phone'),
'user_password' => $this->input->post('password'),
'address' => $this->input->post('address'),
'sex' => $this->input->post('sex')
);
if($this->user_model->insert_user($data))
{
$this->session->set_flashdata('msg','<div class="alert alert-success text-center">You are sucessfully Registered! Please login to access your profile.</div>');
redirect('page/success');
}
else
{
//error
$this->session->set_flashdata('msg','<div class="alert alert-danger text-center">Oops! Error occured. Please try again later.</div>');
redirect('signup/index');
}
}
}
}
?>
我知道我通过调用register()
方法(它只是加载header
,content
和footer
)从另一个控制器(页面。 php)如果验证失败。但我想不出另一种方法。你能帮我找出另一种方式,以便显示用户名吗?谢谢。
有关其他信息,请参阅以下视图:
<?php
$fattr = array('class' => 'form-signin');
echo form_open('/signup/index', $fattr); ?>
<div class="form-group form-inline input-sm">
<label for="name">Nama Lengkap<span style="padding-left: 90px;">:</span></label>
<input class="form-control" name="fname" placeholder="Your Name" type="text" value="<?php echo set_value('fname'); ?>" />
<span class="text-danger"><?php echo form_error('fname'); ?></span>
</div>
<div class="form-group form-inline input-sm">
<label for="username">Username<span style="padding-left: 115px;">:</span></label>
<input class="form-control" name="username" placeholder="Username" type="text" value="<?php echo set_value('username'); ?>" /> <!-- lname -->
<span class="text-danger"><?php echo form_error('username'); ?></span>
</div>
<div class="form-group form-inline input-sm">
<label for="name">Password<span style="padding-left: 118px;">:</span></label>
<input class="form-control" name="password" placeholder="Password" type="password" />
<span class="text-danger"><?php echo form_error('password'); ?></span>
</div>
<div class="form-group form-inline input-sm">
<label for="name">Email<span style="padding-left: 143px;">:</span></label>
<input class="form-control" name="email" placeholder="Email" type="text" value="<?php echo set_value('email'); ?>" />
<span class="text-danger"><?php echo form_error('email'); ?></span>
</div>
<div class="form-group form-inline input-sm">
<label for="name">No. Telepon<span style="padding-left: 109px;">:</span></label>
<input class="form-control" name="phone" placeholder="No. Telepon" type="text" value="<?php echo set_value('phone'); ?>" />
<span class="text-danger"><?php echo form_error('phone'); ?></span>
</div>
<div class="form-group form-inline input-sm sex">
<label for="sex">Jenis Kelamin<span style="padding-left: 95px;">:</span></label>
<select value="<?php echo set_value('sex'); ?>">
<option value="male">Laki-laki</option>
<option value="female">Perempuan</option>
</select>
</div>
<div class="form-group form-inline input-sm alamat">
<label for="alamat">Alamat<span style="padding-left: 135px;">:</span></label>
<textarea id="alamat" name="alamat" rows="5" cols="29" type="text" value="<?php echo set_value('address'); ?>"/></textarea>
</div>
答案 0 :(得分:0)
如果验证失败,您只需要重定向回相同的函数,因此删除对寄存器功能的调用,而是调用redirect('signup/index');
。
然后在你的视图中你只需要回显一个函数codeigniter专门用于此目的:
echo validation_errors('<span class="error">, '</span>');
将它放在页面中的任何位置,靠近顶部的位置可能是最好的。