Form Helper Tutorial in CodeIgniter 4

Reading Time: 6 minutes
12,056 Views

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. By the help of this concept you can even implement Codeigniter form validations.

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

Learn More –

Let’s get started.


CodeIgniter 4 Installation

To create a CodeIgniter 4 setup run this given command into your shell or terminal. Please make sure composer should be installed.

composer create-project codeigniter4/appstarter codeigniter-4

Assuming you have successfully installed application into your local system.


Environment (.env) Setup

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

Either we can do via renaming file as simple as that. Also we can do by terminal command.

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.

Enable Development Mode

CodeIgniter starts up in production mode by default. You need to make it in development mode to see any error if you are working with application.

Open .env file from root.

# 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

This is the tag which is used to open form action in Codeigniter. It opens form opening tag and closing tag.

Once you submit form data with action URL of form then server checks form submit and you can use Codeigniter controller to handle that.

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.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

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.