Form Helper Tutorial in CodeIgniter 4

Share this Article

While working with web forms in codeigniter 4, we have two options to create input elements either in HTML using form inputs and/or by using helper functions of form in codeigniter 4.

Form helper in codeigniter 4 provides several functions which helps to create form inputs. Inputs can any of these like text inputs, password, hidden, select options, file upload input etc.

Inside this article we will see the concept of form helper tutorial in codeigniter 4.

Note*: For this article, CodeIgniter v4.1 setup has been installed. May be when you are seeing, version will be updated. CodeIgniter 4.x still is in development mode.

Let’s get started.


Download & Install CodeIgniter 4 Setup

We need to download & install CodeIgniter 4 application setup to system. To set application we have multiple options to proceed. Here are the following ways to download and install CodeIgniter 4 –

  • Manual Download
  • Composer Installation
  • Clone Github repository of CodeIgniter 4

Complete introduction of CodeIgniter 4 basics – Click here to go. After going through this article you can easily download & install setup.

Here is the command to install via composer –

$ composer create-project codeigniter4/appstarter codeigniter-4

Assuming you have successfully installed application into your local system.


Settings Environment Variables

When we install CodeIgniter 4, we have env file at root. To use the environment variables means using variables at global scope we need to do env to .env

Open project in terminal

$ cp env .env

Above command will create a copy of env file to .env file. Now we are ready to use environment variables.

CodeIgniter starts up in production mode by default. Let’s do it in development mode. So that while working if we get any error then error will show up.

# CI_ENVIRONMENT = production

// Do it to 

CI_ENVIRONMENT = development

Now application is in development mode.


Loading Form Helper into Application

We can load form helper in CodeIgniter 4 in two different ways. Methods to load by –

  • By BaseController.php [parent controller]
  • Loading into any specific controller

Loading by Parent Controller

Open BaseController.php file from /app/Controllers folder and search for $helpers. Add helper into it.

protected $helpers = ["form"];

Loading into Specific Controller

Open any application controller, go inside method and write this code to load form helper.

helper("form");

OR

helper(["form"]);

Form Helper Functions

Form helper provides many functions which helps to create input fields, hidden fields, select options etc.

Let’s see few functions from list.

  • form_open()
  • form_open_multipart()
  • form_hidden()
  • form_input()
  • form_dropdown()
  • form_checkbox()
  • form_radio()
  • form_submit()
  • form_password()
  • form_upload()

To see complete list of available functions, click here.


Tag To Open Form Tag

Syntax

form_open([$action = ''[, $attributes = ''[, $hidden = []]]])

#1 Form Tag

$attributes = ['class' => 'myclass', 'id' => 'myid'];

echo form_open('my-route', $attributes);

HTML

<form action="http://localhost:8080/my-route" method="post" id="myid" class="myclass" accept-charset="utf-8">

#2 Form Tag

$attributes = ['class' => 'myclass', 'id' => 'myid'];

echo form_open_multipart('my-route', $attributes);

HTML

<form action="http://localhost:8080/my-route" method="post" id="myid" class="myclass" accept-charset="utf-8" enctype="multipart/form-data">

Tag For Hidden Field

Syntax

form_hidden($name[, $value = ''])

Hidden Tag

$attributes = ['class' => 'myclass', 'id' => 'myid', 'value' => 'my_value'];

echo form_hidden('hidden_field_name', $attributes);

HTML

<input type="hidden" name="hidden_field_name" class="myclass" id="myid" value="my_value" />

Tag For Input Field

Syntax

form_input([$data = ''[, $value = ''[, $extra = ''[, $type = 'text']]]])

Input Tag

$attributes = ['class' => 'myclass', 'id' => 'myid', 'value' => 'my_value'];

echo form_input('txtname', $attributes);

HTML

<input type="text" name="txtname" class="myclass" id="myid" value="my_value" />

Tag For TextArea Field

Syntax

form_textarea([$data = ''[, $value = ''[, $extra = '']]])

TextArea Tag

$attributes = ['class' => 'myclass', 'id' => 'myid'];

echo form_textarea('txtaddress', $attributes);

HTML

<textarea name="txtaddress" class="myclass" id="myid"></textarea>

Tag For Select Option

Syntax

form_dropdown([$name = ''[, $options = [][, $selected = [][, $extra = '']]]])

Select Option Tag

$options = [        
 'small'  => 'Small Shirt',        
 'med'    => 'Medium Shirt',         
 'large'  => 'Large Shirt',         
 'xlarge' => 'Extra Large Shirt'
]; 

$other_attrs = ["id" => "myid", "class" => "myclass"];

echo form_dropdown('shirts', $options, 'large', $other_attrs);

HTML

<select name="shirts" id="myid" class="myclass">                 
    <option value="small">Small Shirt</option>                 
    <option value="med">Medium Shirt</option>                 
    <option value="large" selected="selected">Large Shirt</option>                 
    <option value="xlarge">Extra Large Shirt</option>         
</select>

Tag For Checkbox Field

Syntax

form_checkbox([$data = ''[, $value = ''[, $checked = FALSE[, $extra = '']]]])

Checkbox Tag

echo form_checkbox('newsletter', 'accept', TRUE);

HTML

<input type="checkbox" name="newsletter" value="accept" checked="checked" />

Tag For Radio Field

Syntax

form_radio([$data = ''[, $value = ''[, $checked = FALSE[, $extra = '']]]])

Radio Button Tag

echo form_radio('myradio', 'male', TRUE);

HTML

<input type="radio" name="myradio" value="male" checked="checked" />

Tag For Input Type File Field

Syntax

form_upload([$data = ''[, $value = ''[, $extra = '']]])

Input Type File Tag

echo form_upload('myfile',["id" => "myid", "class" => "myclass"]);

HTML

<input type="file" name="myfile" id="myid" class="myclass"/>

Input Type Password Field

Syntax

form_password([$data = ''[, $value = ''[, $extra = '']]])

Input Type Password Tag

echo form_password('mypassword',["id" => "myid", "class" => "myclass"]);

HTML

<input type="password" name="mypassword" id="myid" class="myclass"/>

Input Type Submit Button

Syntax

form_submit([$data = ''[, $value = ''[, $extra = '']]])

Input Type Submit Tag

echo form_submit('mysubmit', 'Submit Post!');

HTML

<input type="submit" name="mysubmit" value="Submit Post!" />

We hope this article helped you to learn about Form Helper Tutorial in CodeIgniter 4 in a very detailed way.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Find More on CodeIgniter 4 here

Leave a Comment

What are CSRF Functions in CodeIgniter 4 Tutorial
MySQL Stored Procedure in CodeIgniter 4 Tutorial