用户
 找回密码
 入住 CI 中国社区
搜索
查看: 2927|回复: 4
收起左侧

[已解决] 表单回调函数失败

[复制链接]
发表于 2012-2-22 22:04:06 | 显示全部楼层 |阅读模式
本帖最后由 80后奔三ing 于 2012-2-23 14:12 编辑

如题所示:
我在控制器里监测试图post得数据,并对一数据使用回调检测,代码如下:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends CI_Controller{

        public function __construct()
        {
                parent::__construct();
                $this->load->helper('form');
                $this->load->library('form_validation');
                $this->load->model('User_Model');
        }

        public function index()
        {
                $this->load->view('admin/user_add');
        }

        public function add_user()
        {
                if($this->_form_validation() === FALSE)
                {
                        $this->index();
                }
                else
                {
                        $data['username'] = $this->input->post('username',TRUE);
                        $data['realname'] = $this->input->post('realname',TRUE);
                        $data['sex'] = $this->input->post('sex',TRUE);
                        $data['email1'] = $this->input->post('email1',TRUE);
                        $data['phone'] = $this->input->post('phone',TRUE);
                        $this->User_Model->add_user($data);
                }
        }

        private function _form_validation()
        {
                $this->form_validation->set_rules('username','trim|required|alpha|callback_check_username');
                $this->form_validation->set_rules('realname','trim|required');
                $this->form_validation->set_rules('sex','trim|required|numeric');
                $this->form_validation->set_rules('email1','trim|required|valid_email');
                $this->form_validation->set_rules('phone','trim|required|integer');
                return $this->form_validation->run();
        }

        public function check_username()
        {
                echo 'aaaa';
                var_dump(__LINE__);
                exit();
                return $this->User_Model->check_username($this->input->post('username'));
        }
}

这里始终打印不出任何内容,而自动跳转到了模型里面

<?phpclass User_Model extends CI_Model {
         public function __construct() {   parent::__construct();   $this->load->database();  }          public function get_pwd_by_username ($username) {  $query = $this->db->query("select `name`,`password` from `employee` where `name`={$username}");  return $query->row_array(); }          public function check_username($username) {  $query = $this->db->query("select name from `employee` where `name`={$username}");  return (empty($query->row))? TRUE : FALSE; }          public function add_user($data) { $sql = "insert into `employee` (`name`,`turename`,`sex`,`email1`,`phone`) values({$this->db->escape($data['username'])},{$this->db->escape($data['realname'])},{$this->db->escape($data['sex'])},{$this->db->escape($data['email1'])},{$this->db->escape($data['phone'])})"; $this->db->query($sql); }}
发表于 2012-2-23 08:53:31 | 显示全部楼层
set_rules 第二个参数应该是显示的文字,第三个参数是规则,不知道是不是这个原因
发表于 2012-2-23 12:30:10 | 显示全部楼层
同意楼上,确实是应该放到第三个参数上。
 楼主| 发表于 2012-2-23 14:11:41 | 显示全部楼层
谢谢啊,确实是应该作为第三个参数。
发表于 2012-8-17 21:01:05 | 显示全部楼层
256269683 PHP CI框架 CodeIgnite交流群

本版积分规则