How to Create Form Validation with CodeIgniter

What follows is a "hands on" tutorial for implementing CodeIgniters Form Validation.
In order to implement form validation you'll need three things:
  1. A View file containing a form.
  2. A View file containing a "success" message to be displayed upon successful submission.
  3. A controller function to receive and process the submitted data.
Let's create those three things, using a member sign-up form as the example.

The Form
Using a text editor, create a form called myform.php. In it, place this code and save it to your applications/views/ folder:
<html>
<head>
<title>My Form</title>
</head>
<body>

<?php echo validation_errors(); ?>

<?php echo form_open('form'); ?>

<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>

The Success Page
Using a text editor, create a form called formsuccess.php. In it, place this code and save it to your applications/views/ folder:
<html>
<head>
<title>My Form</title>
</head>
<body>

<h3>Your form was successfully submitted!</h3>

<p><?php echo anchor('form', 'Try it again!'); ?></p>

</body>
</html>

The Controller
Using a text editor, create a controller called form.php. In it, place this code and save it to your applications/controllers/ folder:
<?php

class Form extends CI_Controller {

    function index()
    {
        $this->load->helper(array('form', 'url'));

        $this->load->library('form_validation');

        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('myform');
        }
        else
        {
            $this->load->view('formsuccess');
        }
    }
}
?> 

Just try it!!

Download CodeIgniter New Version 2.0.0.

CodeIgniter is an Application Development Framework - a toolkit - for people who build web sites using PHP. Its goal is to enable you to develop projects much faster than you could if you were writing code from scratch, by providing a rich set of libraries for commonly needed tasks, as well as a simple interface and logical structure to access these libraries. CodeIgniter lets you creatively focus on your project by minimizing the amount of code needed for a given task.

Now, CodeIgniter is currently at Version 2.0.0.
Download here: CodeIgniter Version 2.0.0.

Looking for an older version of CodeIgniter? You can download any of the prior releases below. Please note that archived versions are not maintained. Click here.

Happy trying and learning...:)