0

我已经尝试了我知道的所有可能的方法来解决我的问题。但 form_validation 仍然无法正常工作。

使用 codeigniter 进行表单验证,尝试删除 if else 并运行验证,但它没有验证表单并输入值而不检查。

家庭控制器.php

function __construct()
    {
        parent::__construct();
        $this->load->model('home_model');
        $this->load->database();
        $this->load->helper('url','form');
        $this->load->library('form_validation');
    }

public function register(
 {  if ($this->input->post('reg_button'))
    { $this->form_validation->set_message('vaild_email', 'Please enter a valid email id');

$this->form_validation->set_rules('reg_fname', 'First Name', 'trim|required|min_length[3]|max_length[20]');

$this->form_validation->set_rules('reg_lname', 'Last Name', 'trim|required|min_length[3]|max_length[20]');

$this->form_validation->set_rules('reg_email', 'Email', 'required|vaild_email|is_unique[users.email]');

$this->form_validation->set_rules('reg_email2', 'Confirm Email', 'required|vaild_email|matches[reg_email]');

$this->form_validation->set_rules('reg_password', 'Password', 'trim|required|min_length[5]|max_length[20]');

$this->form_validation->set_rules('reg_password2', ' Confirm Password', 'trim|required|matches[reg_password]');

if ($this->form_validation->run() == FALSE)
                {
                     echo "<script>window.alert('Oops! Error!')</script>";
                }
                else
                {       

    $reg_fname = $this->input->post('reg_fname');
    $reg_lname = $this->input->post('reg_lname');
        $reg_email = $this->input->post('reg_email');
    $reg_email2 = $this->input->post('reg_email2');
    $reg_password = $this->input->post('reg_password');
    $reg_password2 = $this->input->post('reg_password2');
    $user_type = $this->input->post('user_type');

    $result = $this->home_model->insert_data($reg_fname, $reg_lname, $reg_email, $reg_password, $user_type);

            redirect('dashboard_controller', refresh);
`enter code here`           }    
        }   
    }

homemodel.php

   class Home_model extends CI_Model{

        function __construct()
        {
            parent::__construct();

            $this->db = $this->load->database('default', true);

        }

    function insert_data($reg_fname, $reg_lname, $reg_email, $reg_password, $user_type){


                $qry = "INSERT INTO users VALUES ('','$reg_fname','$reg_lname','$reg_email','$reg_password','$user_type')";
               $this ->db->query($qry);


            }

主页视图.php

<form action="<?php echo base_url().'home_controller/login' ?>" method="post" autocomplete="off">

                                 <div class="form-group"> 

                                     <input type="email" class="form-control" name="log_email" placeholder ="Email" required >
                                </div>


                                 <div class="form-group">

                                        <input type="password" class="form-control" name = "log_password" placeholder = "Password">
                                    </div>
                                    <a href="forget.php" id="forgot" class="forgot">Forget Password?</a>

                                  <br>
                                  <input type="submit" class="btn btn-primary btn-block mt-1" value="Login" name = "login_button">

                                    <br>                                                
                                     <a href="#" id="signup" class="signup">Need an account? Register here!</a>

                             </form>
4

1 回答 1

0

更改 echo "window.alert('哎呀!错误!')"; 至

echo validation_errors();
EXIT;

并查看验证错误..为什么它是错误的。

于 2019-01-08T08:57:03.747 回答