自动加载:
$autoload['libraries'] = array('database','form_validation');
控制器:
class Adminlogin extends CI_controller
{
public function new_user()
{
$this->form_validation->set_rules('name','name','required');
$this->form_validation->set_rules('email','email','required|valid_email');
$this->form_validation->set_rules('password','password','required|min_length[8]|max_length[15]');
if ($this->form_validation->run())
{
$Name = $this->input->POST('name');
$Email = $this->input->POST('email');
$Password = $this->input->POST('password');
$data = array('Name' => $Name, 'Email' => $Email, 'Password' => $Password);
$this->load->model('login_model');
$this->login_model->newuser_data($data);
//redirect('public/login_form');
$data['message'] = 'Data Inserted Successfully';
}
else
{
echo "not done";
}
}
如果我从验证中删除条件,那么表格中的工作和数据以及当我在验证中应用条件时,表单不工作数据也不会在没有显示任何错误的情况下运行。
模型:
public function newuser_data($data)
{
$this->db->insert('users', $data);
$query = $this->db->insert_id();
}
视图:
<?php echo form_open('adminlogin/new_user'); ?>
<div class="col-md-4">
<center><h3>Create User!</h3>
<div class="form-group">
<?php echo form_input(['type'=>'name','name'=>'name','placeholder'=>'Name','class'=>'form-control contact-form','value'=>set_value('name')]) ?>
<?php echo form_error('name'); ?>
</div>
<div class="form-group">
<?php echo form_input(['type'=>'email','name'=>'email','placeholder'=>'Email','class'=>'form-control contact-form','value'=>set_value('Email')]) ?>
<?php echo form_error('email'); ?>
</div>
<div class="form-group">
<?php echo form_input(['type'=>'Password','name'=>'password','placeholder'=>'Password','class'=>'form-control contact-form','value'=>set_value('Password')]) ?>
<?php echo form_error('password'); ?>
</div>
<div class="form-group text-right button-submit btn-submit">
<?php echo form_submit(['name'=>'submit','value'=>'Login','class'=>'btn btn-primary']) ?>
</div>
</div>
答案 0 :(得分:1)
class Adminlogin extends CI_controller
{
public function new_user()
{
$this->form_validation->set_rules('name','name','required');
$this->form_validation->set_rules('email','email','required|valid_email');
$this->form_validation->set_rules('password','password','required|min_length[8]|max_length[15]');
if ($this->form_validation->run()!=FALSE)
{
$Name = $this->input->POST('name');
$Email = $this->input->POST('email');
$Password = $this->input->POST('password');
$data = array('Name' => $Name, 'Email' => $Email, 'Password' => $Password);
$this->load->model('login_model');
$this->login_model->newuser_data($data);
//redirect('public/login_form');
$data['message'] = 'Data Inserted Successfully';
}
else
{
echo "not done";
}
}
if条件改变
if ($this->form_validation->run())
到
if ($this->form_validation->run()!=FALSE)
答案 1 :(得分:0)
将此更改为
if ($this->form_validation->run())
到
if ($this->form_validation->run() != FALSE)
答案 2 :(得分:0)
更改行
if ($this->form_validation->run())
到
if ($this->form_validation->run()===TRUE)
答案 3 :(得分:0)
更改您的陈述
发件人强>
if ($this->form_validation->run())
要强>
if ($this->form_validation->run() == TRUE)
或强>
if ($this->form_validation->run() != FALSE)
答案 4 :(得分:0)
尝试使用此控制器
class Adminlogin extends CI_controller
{
public function new_user()
{
$this->form_validation->set_rules('name','name','required');
$this->form_validation->set_rules('email','email','required|valid_email');
$this->form_validation->set_rules('password','password','required|min_length[8]|max_length[15]');
if ($this->form_validation->run()===FALSE)
{
echo 'Not done';
}
else
{
$Name = $this->input->POST('name');
$Email = $this->input->POST('email');
$Password = $this->input->POST('password');
$data = array('Name' => $Name, 'Email' => $Email, 'Password' => $Password);
$this->load->model('login_model');
$this->login_model->newuser_data($data);
$data['message'] = 'Data Inserted Successfully';
}
}