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

[已解决] 表单验证类如何获取表单数据

[复制链接]
发表于 2013-6-21 13:31:14 | 显示全部楼层 |阅读模式
如题,通过表单验证类验证表单后,如何获取表单中的数据,以便做数据库操作之类?

看了用户手册,在表单验证的config文件中可以设定每个字段的回调函数从而达到验证的自定义验证的目的,但是感觉上都只能对当前的字段进行操作。
例如一个用户注册,在config中配置如下:
'admin/login'=>array(
                                array(                //对username字段的验证规则
                                        'field'=>'username',
                                        'label'=>'username',
                                        'rules'=>'trim|required|alpha_numeric|max_length[20]|xss_clean'),
                                array(                //password字段
                                        'field'=>'password',
                                        'label'=>'password',
                                        'rules'=>'trim|required|alpha_dash|max_length[20]|min_length[6]|xss_clean|callback_validateUser')
                        ),

在login函数中
if ($this->form_validation->run() == FALSE) { ......        }
else { ....}
可是,如果希望在该函数中使用这两个字段该如何调用呢。
我现在想到的只能是重新取一次,
$user = $this->input->post('username');
$password = $this->input->post('password');
不过这明显不科学。。

求教,谢谢




发表于 2015-1-7 14:03:40 | 显示全部楼层
本帖最后由 Closer 于 2015-1-7 14:19 编辑
锋之路 发表于 2015-1-7 13:43
版主 那个我想问下 表单的数据 如何获取呢  网上查了说是用$this->input->post('name')  可是我用了 还是 ...

form_open() 你可能要重新看一下他的用法
他是幫你製作一個 <form> 標籤及其參數
而不是一個啟用 form 的函式
他不像 session_start() 那種
使用前需要開啟

另外,表單驗證類你沒有將它啟用
標準的用法大概像這樣:
PHP复制代码
 
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class Test extends CI_Controller {
 
        function __construct() {
                parent::__construct();
                //載入 form,這樣 form_open() 才能用
                $this->load->helper('form');
                //如果在 config 有寫成一個驗證檔案才載入
                $this->load->library('form_validation');
        }
 
        public function index(){
                $this->form_validation->set_rules(
                        'content', '發問', 'trim|required|max_length[200]');
                $this->form_validation->set_rules(
                        'secret', '悄悄話', 'max_length[1]|numeric');
 
                if ($this->form_validation->run() == FALSE){
                        //審核未過,只顯示 view
                        $this->layout->view('v_test', $data);
                }else{
                        //審核通過
                        //取得所有 POST 值,並過濾 XSS 語法
                        $test = $this->input->post(NULL, TRUE);
                       
                        //寫入資料表並導頁
                        $ad = array(
                                'name'=>$test['name'],
                                'password'=>$test['password'],
                                'email'=>$test['email'] //記得別多打一個逗號
                        );
                        $this->db->insert('test',$ad);
                        redirect('', 'refresh'); //導向首頁
                }
        }
       
}
 
?>
 
复制代码

 
发表于 2015-1-7 13:43:17 | 显示全部楼层
Closer 发表于 2015-1-7 13:01
看不懂這主題的問題點在哪

版主 那个我想问下 表单的数据 如何获取呢  网上查了说是用$this->input->post('name')  可是我用了 还是数据还是不能写入数据库这个是我的表单
<html>
<head>
<title>My Form</title>
</head>
<body>

<?php

echo validation_errors();
//判断是否由错误 是一个表单辅助函数 是会返回验证器送回的所有错误信息
//如果没有错误信息,它将返回空字符串 就是没有出现错误提醒

?>

<?php

echo form_open('form');//打开form文件 进行一些数据的处理

?>
<form  action="formadd.php" method="post">
<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>

</body>
</html>

这个是我的控制器

class Formadd extends CI_Controller {

   function __construct()
    {
      parent::__construct();
      $this->load->database();
     }

        function add()
  {
          $user=$this->input->post('username');
      $password=$this->input->post('password');
          $email=$this->input->post('email');
          $data=array(
                        'name'=>$user,
                                'password'=>$password,
                                'email'=>$email,
                 );         
            $this->db->insert('test',$data);
               
          
  }



发表于 2015-1-7 14:41:46 | 显示全部楼层
本帖最后由 Closer 于 2015-1-7 14:45 编辑
锋之路 发表于 2015-1-7 14:38
谢谢版主 还有几个问题1、是不是每个表单是要用  if ($this->form_validation->run() == FALSE)这个方法 ...

關於 $this->form_validation->run() == FALSE
你有用 CI 的表單驗證類才需要使用
單純表單類是不需要這個的 (如果你要用 javascript 過濾的話)

$test=$this->input->post(NULL,TRUE);
是用來取得所有 POST
好處是,你不需要為所有的 <input> 都寫一條接收 POST 的代碼
差別就在於

$test=$this->input->post(NULL,TRUE);
//$test['name'], $test['phone']

$name = $this->input->post('name', TRUE);
$phone = $this->input->post('phone', TRUE);
//$name, $phone

 
发表于 2013-6-21 13:53:59 | 显示全部楼层
$this->input->post
就是这样啊,还要怎样!
 楼主| 发表于 2013-6-22 07:20:17 | 显示全部楼层
难道实用表单验证类时,不能已经保存了这些表单值吗。
例如我们用表单验证类做了trim处理之类,总不会说验证完了,要重新取一遍表单值,然后重新trim吧。
求教
发表于 2015-1-7 11:53:38 | 显示全部楼层
楼主现在解决了吗
发表于 2015-1-7 13:01:06 | 显示全部楼层
本帖最后由 Closer 于 2015-1-7 13:46 编辑
锋之路 发表于 2015-1-7 11:53
楼主现在解决了吗

看不懂這主題的問題點在哪



後來我看懂問題了
就是 $this->form_validation->run() 尚未執行前
他的表單驗證類就不會執行
也就不會檢查和過濾資料
雖然這是理所當然的事...

這不是解不解決的問題,而是邏輯問題
你只能讓 $this->form_validation->run() 過濾完資料後再取 POST

 
发表于 2015-1-7 14:38:45 | 显示全部楼层
本帖最后由 锋之路 于 2015-1-7 14:41 编辑
Closer 发表于 2015-1-7 14:03
form_open() 你可能要重新看一下他的用法
他是幫你製作一個  標籤及其參數
而不是一個啟用 form 的函式

谢谢版主 还有几个问题1、是不是每个表单是要用  if ($this->form_validation->run() == FALSE)这个方法呢


发表于 2015-1-7 14:51:14 | 显示全部楼层
Closer 发表于 2015-1-7 14:41
關於 $this->form_validation->run() == FALSE
你有用 CI 的表單驗證類才需要使用
單純表單類是不需要這個 ...

对了 为什么是null呢 而不是别的呢

本版积分规则