欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

CodeIgniter表单验证方法实例详解

程序员文章站 2024-04-01 19:22:10
本文实例讲述了codeigniter表单验证方法。分享给大家供大家参考,具体如下: 1.在d:\codeigniter\system\application\views目...

本文实例讲述了codeigniter表单验证方法。分享给大家供大家参考,具体如下:

1.在d:\codeigniter\system\application\views目录下写一个视图文件myform.php

<html>
<head>
<title>my form</title>
</head>
<body>
<?php echo $this->validation->error_string;?>
<?php echo form_open('form/index');?>
<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>

然后再写一个视图文件formsuccess.php

<html>
<head>
<title>my form</title>
</head>
<body>
<h3>your form was successfully submitted!</h3>
<p><?=anchor('form', 'try it again!'); ?></p>
</body>
</html>

2.在d:\codeigniter\system\application\controllers目录下写一个控制器文件form.php

<?php
class form extends controller{
 function index(){
 $this->load->helper(array('form','url'));
 $this->load->library('validation');
   $rules['username'] = "required";
    $rules['password'] = "required";
    $rules['passconf'] = "required";
    $rules['email'] = "required";
    $this->validation->set_rules($rules);
 //    $this->validation->set_error_delimiters('<div class="error">','</div>');
 $fields['username'] = 'username';
 $fields['password'] = 'password';
 $fields['passconf'] = 'password confirmation';
 $fields['email'] = 'email address';
 $this->validation->set_fields($fields);
   if ($this->validation->run()==false) {
   $this->load->view('myview/myform');
   }else {
   $this->load->view('myview/formsuccess.php');
   }
 }
}
?>

3.http://localhost:8888/index.php/form/index访问一下

ok,结果都出来了

更多关于php相关内容感兴趣的读者可查看本站专题:《codeigniter入门教程》、《ci(codeigniter)框架进阶教程》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家php程序设计有所帮助。