说明:- 它直接转到else部分,而不执行验证部分。 我在autoload.php中添加了“ form_validation”库和“ form”帮助器。 Register.php包含视图部分。 Main_Controller.php包含功能user_register。 我已经尝试了两种方式: 1. set_rules() 2. form_validation.php(外部文件)
观看部分:
<?php include('Header.php'); ?>
<!DOCTYPE html>
<html>
<head>
<title>User Registration</title>
</head>
<body>
<div class="container" style="margin-top:20px;border:1px solid black;width:50%;">
<h1>Register Here</h1>
<?php echo form_open('Main_Controller/user_register'); ?>
<div class="row">
<div class="col-lg-6">
<div class="form-group">
<label for="Name">Name:</label>
<?php echo form_input(['class'=>'form-control','placeholder'=>'Enter Name','name'=>'Full_Name','value'=>set_value('Full_Name')]); ?>
</div>
</div>
<div class="col-lg-6" style="margin-top:40px;">
<?php echo form_error('Full_Name'); ?>
</div>
</div>
<div class="row">
<div class="col-lg-6">`enter code here`
<div class="form-group">
<label for="email">Email:</label>
<?php echo form_input(['class'=>'form-control','type'=>'text','placeholder'=>'Enter Email',
'name'=>'Email','value'=>set_value('Email')]); ?>
</div>
</div>
<div class="col-lg-6" style="margin-top:40px;">
<?php echo form_error('Email'); ?>
</div>
</div>
<div class="row">
<div class="col-lg-6">
<div class="form-group">
<label for="Mobile">Mobile No:</label>
<?php echo form_input(['class'=>'form-control','placeholder'=>'Enter Mobile No','name'=>'Mobile',
'value'=>set_value('Mobile')]); ?>
</div>
</div>
<div class="col-lg-6" style="margin-top:40px;">
<?php echo form_error('Mobile'); ?>
</div>
</div>
<div class="row">
<div class="col-lg-6">
<div class="form-group">
<label for="Employee_Id">Employee Id:</label>
<?php echo form_input(['class'=>'form-control','placeholder'=>'Enter Employee Id',
'name'=>'Employee_Id','value'=>set_value('Employee_Id')]); ?>
</div>
</div>
<div class="col-lg-6" style="margin-top:40px;">
<?php echo form_error('Employee_Id'); ?>
</div>
</div>
<div class="row">
<div class="col-lg-6">
<div class="form-group">
<label for="Username">Username:</label>
<?php echo form_input(['class'=>'form-control','placeholder'=>'Enter Username','name'=>' Username','value'=>set_value('Username')]); ?>
</div>
</div>
<div class="col-lg-6" style="margin-top:40px;">
<?php echo form_error('Username'); ?>
</div>
</div>
<div class="row">
<div class="col-lg-6">
<div class="form-group">
<label for="pwd">Password:</label>
<?php echo form_password(['class'=>'form-control','type'=>'password','placeholder'=>'Enter Password','name'=>'Password','value'=>set_value('Password')]); ?>
</div>
</div>
<div class="col-lg-6" style="margin-top:40px;">
<?php echo form_error('Password'); ?>
</div>
</div>
<div class="row">
<div class="col-lg-6">
<div class="form-group">
<label for="Confirm Password">Confirm Password:</label>
<?php echo form_password(['class'=>'form-control','type'=>'password','placeholder'=>'Enter Confirm Password','name'=>'Confirm_Password','value'=>set_value('Confirm_Password')]); ?>
</div>
</div>
<div class="col-lg-6" style="margin-top:40px;">
<?php echo form_error('Confirm_Password'); ?>
</div>
</div>
<?php echo form_submit(['type'=>'submit','class'=>'btn btn-primary','value'=>'Submit']); ?>
<?php echo form_reset(['type'=>'reset','class'=>'btn btn-default','value'=>'Reset']); ?>
<br><br>
</div>
</body>
</html>
Controller (Main_Controller) :-
public function user_register()
{
$this->form_validation->set_rules('Full_Name','Full Name','required|alpha');
$this->form_validation->set_rules('Email','Email','required');
$this->form_validation->set_rules('Mobile','Mobile Number','required');
$this->form_validation->set_rules('Employee_Id','Employee Id','required');
$this->form_validation->set_rules('Username','Username','required');
$this->form_validation->set_rules('Password','Password','required');
$this->form_validation->set_rules('Confirm_Password','Confirm Pssword','required');
$this->form_validation->set_error_delimiters('<div class="text-danger">','</div>');
//$this->load->view('');
if($this->form_validation->run())
{
$post=$this->input->post();
$this->load->model('My_Model');
if($this->My_Model->reg($post))
{
echo "Registration Successfull";
}
else
{
echo "Registration Failed";
}
}//if
else
{
return redirect('Main_Controller/register');
}
}//function
答案 0 :(得分:0)
重定向可清除验证错误。 试试这个
if ($this->form_validation->run()) {
echo "Registration Successfull";
} else {
$this->session->set_flashdata('error', validation_errors());
redirect('Main_Controller/register');
}