【问题标题】:form validation not working in codeigniter as expected表单验证未按预期在 codeigniter 中工作
【发布时间】:2018-01-18 07:42:37
【问题描述】:

我刚开始使用 codeigniter 并遇到表单验证问题

这是我的代码 控制器:

public function indexnew(){
        $data['title'] = array('welcome'=>'text here');
        $this->load->helper('form');    

        $this->load->library('form_validation');

         $this->form_validation->set_rules('username', 'Username', 'required');

        $this->load->view('come/come',$data);
    }

查看:

<form method="POST" action="form">
        <h5>Username</h5>
        <input type="text" name="username" value="" size="50" />

        <h5>Password</h5>
        <input type="text" name="password" value="" size="50" />

        <h5>Password Confirm</h5>
        <input type="text" name="passconf" value="" size="50" />

        <h5>Email Address</h5>
        <input type="text" name="email" value="" size="50" />

        <div><input type="submit" value="Submit" /></div>
    </form>

添加了用户名验证,但它不起作用

请有人告诉我我做错了什么。

【问题讨论】:

  • 给表单属性action="controller_name/indexnew".

标签: php codeigniter validation


【解决方案1】:

你必须在你的行动中提及路径

<form method="POST" action="form">

应该是

<form method="POST" action="<?php echo site_url('controller_name/indexnew')?>">

在 controller_name 处写下您的控制器名称。

你还必须在 config.php 中设置 base_url

$config['base_url'] = '';

我希望它有效....!!!!!!

【讨论】:

    【解决方案2】:

    你可以试试这个代码来解决你的问题:

    Controller.php

    public function indexnew(){
            $data['title'] = array('welcome'=>'text here');
            $this->load->helper('form');    
            $this->load->library('form_validation');
            $this->form_validation->set_rules('username', 'username', 'required');
            if($this->form_validation->run()) {
                // Add here other stuff
                redirect('page_name');
            } else {
                $this->load->view('come/come',$data);
            }
        }
    

    我希望它会有所帮助。

    【讨论】:

      猜你喜欢
      • 2019-09-01
      • 2018-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多