CI(6)表单验证机制

1、HTML页面:

<?php
    if (!defined('BASEPATH') ) exit ('No direct script access allowed');
    $this->load->helper('form');
?>

<h2>Create a news item</h2>

<?php echo validation_errors(); ?> 

<?php echo form_open('news/create') ?>

  <label for="title">Title</label>
  <input type="input" name="title" /><br />

  <label for="text">Text</label>
  <textarea name="text"></textarea><br />

  <input type="submit" name="submit" value="提交" />

</form>

注:form_open() 表单辅助函数 提供,用来提供表单元素和一些额外功能,例如 添加隐藏的 安全类
    validation_errors() 用来报告表单验证中出现的错误信息

2、 控制器:需要做两件事。
    一、是检查表单是否被提交;
    二、另一件是检查提交的数据是否能够通过验证规则。( 需要用到  表单验证  库)
public function create()
{
//表单辅助函数和表单验证库
$this->load->helper('form');
$this->load->library('form_validation');
$data['title'] = 'Create a news item';
//set_rules() 方法包含三个参数:输入域的名称、错误信息的名称、错误信息的规则——在这里的规则是输入内容的文本域必填。
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('text', 'text', 'required');
if ($this->form_validation->run() === FALSE)
{
//验证失败,返回原页面
$this->load->view('templates/header',
$data);$this->load->view('news/create');
$this->load->view('templates/footer');
 }
else
{
//验证成功,执行操作,返回成功页面
$this->news_model->set_news();
$this->load->view('news/success');
}
}

3、数据处理函数model层:

public function set_news()
{
  $this->load->helper('url');
  $slug = url_title($this->input->post('title'), 'dash', TRUE);
  $data = array(
    'title' => $this->input->post('title'),
    'slug' => $slug,
    'text' => $this->input->post('text')
  );
  return $this->db->insert('news', $data);
}
注:post() ,它是由 输入类提供的。这个方法可以确保数据是被过滤过(sanitized)的,从而保护你不被其他人恶意攻击



扩展:

    1、 required必填项的其他设置
$this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[12]');
$this->form_validation->set_rules('password', 'Password', 'required|matches[passconf]');
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');

上面的代码设置了一组规则:

1、用户名表单域长度不得小于5个字符以及大于12个字符。

2、密码表单域必须跟密码确认表单域的数据一致。

3、电子邮件表单域必须是一个有效邮件地址。


    2、form表单实现伪异步提交

        (1)HTML页面:

    <iframe name="project_hidden_frame" id="project_hidden_frame" frameborder="0" style="display: none;"></iframe>

    <form action="<?= $root_path.'/index.php/helloworld/login'; ?>" method="post" enctype="multipart/form-data" target="project_hidden_frame">

        <div class="user-name">

            <label>用户名:</label><input type="text" name="username"/>

        </div>

        <div class="user-password">

            <label>密码:</label><input type="password" name="password"/>

        </div>

        <input type="submit" value="提交"/>

    </form>


<script type="text/javascript">

function projectInvestCallback(data){

console.info(data.username);

}

</script>


 (2)控制器对应的函数login:(获取form表单提交的数据,并返回给前端)

public function login(){

//获取表单数据

$destination['username'] = $this->input->post('username');

$destination['password'] = $this->input->post('password');

//返还给前端

$result = '<script type="text/javascript">window.parent.projectInvestCallback(' . json_encode($destination) . ')</script>';

echo $result;

}


















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值