Working with forms is common for anyone in the web development field. Forms are used to capture information for one purpose or another. Be it user login credentials, or customer order details, forms are at the heart of interaction in websites and applications across the Internet. The CodeIgniter PHP framework comes with several built-in helper libraries, that assist in rapid web development. One of them is the form helper. In this post, I’ll cover some of the common methods available from the form helper library…
If you enjoy the content written here, by all means, share this blog and your favorite post(s) with others who may benefit from or like it as well. Since coffee is my favorite drink, you can even buy me one if you would like!
CodeIgniter Form Helper – What are some advantages to using the form helper?
By using the form helper, you gain many advantages that you don’t have using regular HTML form elements (or at least without additional overhead work).
For starters, and most importantly, if you have CSRF (Cross Site Request Forgery) protection enabled in your application, the
form_open() method automatically creates a hidden
input field and sets the
csrf_field() methods in this element. Alternatively, you can use the
csrf_field() method and accomplish the same.
Next, and perhaps more of a personal benefit, you can code the form elements and any attributes in PHP.
CodeIgniter Form Helper – What’s available?
You will find all the common input types that you would for standard HTML form elements (e.g.,
button, etc…), available as CodeIgniter form helper methods.
The example form (shown below) for this post includes the below methods:
form_open()– Creates the actual form itself as if
<form>elements are used.
form_input()– Used to create text, numeric, and date
<input />data fields.
form_dropdown()– This method creates the
form_label()– If you want a
<label>tag element, this method provides it.
form_submit()– This method will include a
<input />type in the form for processing.
form_button()– Used for an HTML
form_close()– Creates the closing
set_value()– Sets the value of an
<input />or text
As shown in the PHP code pictured above, using the form helper methods, yields this raw HTML in the browser:
Which in turn, provides us with the nice web form below:
Consider making a small donation on my behalf as I continue to provide useful and valuable content. Thank you.
CodeIgniter Form Helper – methods and attributes.
I’ll briefly cover some of the form helper methods and their parameters in this section.
Staring with the
form_open() method, the first parameter
base_url(/walks/create) is the
action attribute of the form that processes the form upon submittal along with setting the
method attribute to
post. Notice the second parameter is a PHP associative array of key/value pairs. In this case, I am setting the
class attribute to Bootstrap 4 classes. You can provide the second parameter as an associative array or as a string. So this would have easily been valid as well:
'class="shadow p-3 mb-5 bg-white rounded"'.
Moving to the
form_input() methods, I use associative arrays exclusively providing:
Next in the
form_label() method, I provide 2 parameters: the label text and the
for attribute. You can specify an optional associative array or string of any additional attributes as well.
form_options() method is quite involved (or it was for me) so here is the rundown of the parameters I am using in this tag:
$shoesan associative array of options (the
<option>tag) generated from a database query (not shown).
set_value('shoe_id')– using the
set_value()method to set the actual values.
['id' =>; 'shoe_id']– associative array for the
idattribute and accompanying value.
Like I mentioned previously in the post, I prefer using PHP methods with parameters coding all of the HTML elements.
CodeIgniter Form Helper – How to load the form helper?
Loading the form helper library can be done either in a controller’s constructor method for those controllers in which you need it. Or include it in the BaseController’s (which all controllers extend)
protected $helpers array, therefore having availability in every controller you create.
I currently use this means of loading the form helper, although I am not sure if it is necessary. Most likely, not every controller will process form data. However, it is one less thing to worry about or remember so that is the plus I feel outweighs any negative drawbacks. YMMV
Closing and Recommended Reading
If you are using CodeIgniter for PHP development and are not using the form helper library you should definitely check it out.
As always, if you see any mistakes in the code or have any questions, please leave a comment below, and thanks for reading!
Visit any of the below PHP-related blog posts I have written if you are so inclined. Please share them with others who would benefit from the content as well.
- PHP date() function for common date formats
- PHP trim functions: trim, rtrim, and ltrim
- PHP empty() function use with MySQL NULL
- Use MySQL BLOB column with PHP to store .pdf file
Like what you have read? See anything incorrect? Please comment below and thank you for reading!!!
A Call To Action!
Thank you for taking the time to read this post. I truly hope you discovered something interesting and enlightening. Please share your findings here, with someone else you know who would get the same value out of it as well.
Visit the Portfolio-Projects page to see blog post/technical writing I have completed for clients.
Be sure and visit the “Best Of” page for a collection of my best blog posts.
Josh Otwell has a passion to study and grow as a SQL Developer and blogger. Other favorite activities find him with his nose buried in a good book, article, or the Linux command line. Among those, he shares a love of tabletop RPG games, reading fantasy novels, and spending time with his wife and two daughters.
Disclaimer: The examples presented in this post are hypothetical ideas of how to achieve similar types of results. They are not the utmost best solution(s). The majority, if not all, of the examples provided, are performed on a personal development/learning workstation-environment and should not be considered production quality or ready. Your particular goals and needs may vary. Use those practices that best benefit your needs and goals. Opinions are my own.