CodeIgniter表單驗證方法執行個體詳解_php執行個體

來源:互聯網
上載者:User

本文執行個體講述了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

<?phpclass 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程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.