This article describes the CodeIgniter form verification method through examples. Share it with everyone for your reference, the details are as follows:
1. Write a view file myform.php in the D:CodeIgnitersystemapplicationviews directory
<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>
Then write a view file 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. Write a controller file form.php in the D:CodeIgnitersystemapplicationcontrollers directory
<?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. Visit http://localhost:8888/index.php/form/index
Ok, the results are out
Readers who are interested in more PHP-related content can check out the special topics of this site: "Introduction to codeigniter tutorial", "CI (CodeIgniter) framework advanced tutorial", "php date and time usage summary", "php object-oriented program" Design introductory tutorial", "php string (string) usage summary", "php mysql database operation introductory tutorial" and "php common database operation skills summary"
I hope this article will be helpful to everyone in PHP programming.