If you are using CodeIgniter, as well as any other PHP framework, you may notice that building CRUD forms is one of the most bother and routine tasks. Probably 80% of general web applications uses CRUD (create/read/update/delete).
In CodeIgniter, you can use any of the form generation libraries .
One of my preferred libraries is Form Generation Library by Frank Michel (see @macigniter’s thread here ). It allows you to create clean XHTML forms with CodeIgniter. See demo here .
So, in order to use this library, you need to download it from here (library files only is enough if you don’t want the entire site structure).
Make sure to follow the installation steps, as shown here . That means unzipping the package, configuring config/form.php and moving config/form.php
and libraries/form.php
to the right directories.
Let’s assume we want to create a simple form for my Time tracking utility.
This simple form has:
So, I created a Project controller class with an edit() function, and added the following method:
function edit() { $this->load->library('form'); $this->form->open('project/edit', 'project_edit_form') ->fieldset('Project') ->text('name', 'Project Name', 'max_length[40]') ->text('description', 'Description', 'max_length[40]') ->textarea('notes', 'Notes', 'trim', "Write your project notes here") ->indent(150) ->submit('Submit', 'sub') ->onsuccess('redirect', 'project/index') ->nobr(); $this->data['form'] = $this->form->get(); $this->data['errors'] = $this->form->errors; $this->load->view('project/edit', $this->data ); }
Then, we need to create the view. For that purpose, I created the edit.php file under application/view/project folder.
<?php Header('Cache-Control: no-cache'); Header('Pragma: no-cache'); $this->load->view('header', $data); ?> <h2 > Project</h2> <?php echo $form ?> <?php $this->load->view('footer', $data); ?>
The important code line there is only <?php echo $form ?> since it displays the form we previously generated. However, the other pieces of code display a generic header and footer part, you can discard that for your tests.
just need to open your browser, for example at: http://localhost/project/edit to see the results.
By using this Form Generation library you can create advanced forms, too. For example you can create forms that use checkboxes, radio buttons, multiple lists, etc. I’d recommend to download the library including CI and user guide, from here and look for views/welcome.php example to see an advanced version.