程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP綜合 >> CodeIgniter表單驗證方法實例詳解

CodeIgniter表單驗證方法實例詳解

編輯:PHP綜合

本文實例講述了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程序設計有所幫助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved