Case Study : Create a Form and its validation with CodeIgniter.

System Requirements : CodeIgniter PHP Framework, download here. And Webserver Package.

User Requirements : PHP skills, Mysql skills, HTML skills, and (few, enough) CodeIgniter skills.

When developing a web application, like it or not sure we will often use a form. Form is very useful to make an interface for inputing data into the database. In order to prevent the data input that saved to database is correct, or desirable, we need a validation. In PHP programming language, in general, validation is made by using the ereg functions, using regular expression (regex). And believe me, regex is very complicated. Hahahah

With CodeIgniter, validation is much easier to make when compared with normal php.

Ohya, this is a server-side validation. Clearly, validation is performed from the server side so that automatic validation system will be more secure than the client-side validation (usually made with javascript)

Okay, here we go…

Step 1 : CI Configuration

  1. Open the config.php file located in the system-application-config-config.php folder
  2. Change base url, adjust to the location where your CI folders are (ci extract folder). For example : Your CI folder is in the www/ciso change the line $config['base_url']="";
    $config['base_url'] = "http://localhost/ci/";
  3. Database setting. Open database.php file located in the same folder with config.php. Change hostname, username, password, dan nama database. Adjust according your Mysql setting. For Example :$db['default']['hostname'] = "localhost";
    $db['default']['username'] = "root";
    $db['default']['password'] = "";
    $db['default']['database'] = "db_code";

Step 2 : Prepare the database

  1. Create a database with name db_code (via phpmyadmin)
  2. Prepare a table with name tb_student (for the structure, see picture below)

    tb_student's structure

  3. Ok, we’re done with database !

Step 3 : Make Controller

  1. Type the following script with your favorite text editor
    class Student extends Controller {function Student(){
    function index() {
    $data['title'] = 'Add New Student';
    $data['message'] = '';
    $data['action'] = site_url('student/addStudent');
    $this->load->view('addStudent_view', $data);
    function addStudent(){
    $data1['title'] = 'Add New Student';
    $data1['action'] = site_url('student/addStudent');
    // run the validation
    if ($this->validation->run() == FALSE){
    $data['message'] = '';
    // saving data
    $data= array('id' => $this->input->post('id'),
    'nama' => $this->input->post('nama'),
    'email' => $this->input->post('email')
    $this->db->insert('tb_student', $data);
    $data1['message'] = 'Sukses';
    // load view
    $this->load->view('addStudent_view', $data1);
    function _set_fields(){
    $fields['id'] = 'id';
    $fields['nama'] = 'nama';
    $fields['email'] = 'email';
    // rules for validation
    function _set_rules(){
    $rules['id'] = 'trim|required|numeric';
    $rules['nama'] = 'trim|required';
    $rules['email'] = 'trim|required|valid_email';
    $this->validation->set_message('required', '* required');
    $this->validation->set_message('numeric', '* numeric only');
    $this->validation->set_message('valid_email', '* email not valid');
    $this->validation->set_error_delimiters('<p>', '</p>');
  2. Save in the system-application-controllers folder, with the name student.php

Step 4 : Create View

  1. Type the script below,
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
    <html xmlns="">
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Form Validation CI</title>
    <h1><?php echo $title; ?></h1>
    <?php if (isset($message)) echo $message; ?>
    <form method="post" action="<?php echo $action; ?>">
    <td valign="top">ID*</td>
    <td><input type="text" name="id" value="<?php echo $this->validation->id; ?>"/>
    <span style="color:red;"> <?php echo $this->validation->id_error; ?></span></td>
    <td valign="top">Nama<span style="color:red;">*</span></td>
    <td><input type="text" name="nama" value="<?php echo $this->validation->nama; ?>"/>
    <span style="color:red;"> <?php echo $this->validation->nama_error; ?></span></td>
    <td valign="top">Email<span style="color:red;">*</span></td>
    <td><input type="text" name="email" value="<?php echo $this->validation->email; ?>"/>
    <span style="color:red;"> <?php echo $this->validation->email_error; ?></span></td>
    <td><input type="submit" value="Save"/></td>
  2. Save in the system-application-views folder, with the name addStudent_view.php

Step 5 : Testing Code

  1. Open your browser, goto http://localhost/YourCIFolder/student
  2. You will see a display like the following,

    Input Form

  3. Try fill out the form with incorrect input (example, ID : AAA; Nama : (empty); Email : AAA)

    Wrong Input (example)

  4. Click Save. Then you will see the error is displayed as shown below,

    Validation Confirmation

  5. Now try to fill out the form with the correct input, as in the picture below, then Save. You will see a confirmation of success :D Also check your database, you should see the data you just entered in the tb_student :D

    Correct Input (example)

    and this…

    Success Confirmation

p.s. Maybe there will be a question, this is a MVC, but, where is the model?

And the answer is : in CI, we are not always required to use models. Using code like $this->db->insert('tb_student', $data); is enough, according to complexity of your code..

Okay, (always) Happy Coding :D
Hal yang juga menarik:

Hak Cipta

Semua skrip dan teknik dalam artikel di boleh digunakan sebagaimana kehendakmu tanpa perlu mencantumkan sumber. Kamu tidak boleh mengkopi seluruh artikel, dalam Bahasa Indonesia ataupun diterjemahkan ke dalam bahasa lain.

Related Articles:

Artikel terkait:


About the author

obviously, a girl. with sweet smile, off course. turning her 21 years trapped on Informatics departement and just started to having some crush with it lately. she uses wordpress. she loves php. she lo

1 Comment

Tinggalkan Balasan

Alamat surel Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *

x( X-( B-) ;-) :wink: :twisted: :roll: :oops: :mrgreen: :lol: :idea: :evil: :cry: :arrow: :D :?: :-| :-x :-o :-P :-D :-? :-)) :-) :-( :-& :) :( :!: 8-O 8-) 8) (Русский) (yahoo) (worship) (woot) (wave) (unsure) (tongue) (thinking) (tears) (taser) (smileydance) (sleeping) (sick) (scenic) (rofl) (rock) (party) (panic) (okok) (nottalking) (ninja) (music) (muscle) (muhaha) (money) (mmm) (lonely) (lol) (lmao) (idiot) (hungry) (highfive) (heart_beat) (heart) (headspin) (hassle) (haha) (gym) (griltongue) (goodluck) (girlkiss) (funkydance) (fish_hit) (eyeroll) (evilsmirk) (evil_grin) (drinking) (doh) (devil) (dance) (cry) (cozy) (coffee) (brokenheart) (bringit) (blush) (bigeyes) (beer) (banana_rock) (banana_ninja) (banana_cool) (applause) (annoyed) (angry) (K) (: