您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關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');?> <h6>Username</h6> <input type="text" name="username" value="" size="50" /> <h6>Password</h6> <input type="text" name="password" value="" size="50" /> <h6>Password Confirm</h6> <input type="text" name="passconf" value="" size="50" /> <h6>Email Address</h6> <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> <h4>Your form was successfully submitted!</h4> <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,結果都出來了
感謝各位的閱讀!關于“CodeIgniter如何實現表單驗證”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。