Building Forms in CodeIgniter

CodeIgniter is a powerful PHP framework that simplifies the process of building web applications. One of the essential aspects of web development is creating forms that allow users to input and submit data. In this article, we will explore how to build forms in CodeIgniter.

Getting Started

Before we dive into creating forms, make sure you have CodeIgniter installed and set up on your development environment. Once you have CodeIgniter up and running, follow the steps below to build a form.

Step 1: Create a Controller

To start building our form, we need to create a new controller that will handle our form submissions and perform any necessary actions. Open your favorite code editor and create a new file called FormController.php in the controllers directory.

class FormController extends CI_Controller {
    public function index() {

    public function submit() {
        // Handle form submission logic

In the above code, we define a FormController class that extends CI_Controller. We have two methods: index() and submit(). The index() method loads our form view, and the submit() method handles form submissions.

Step 2: Create a View for the Form

Next, we need to create a view file that will render our form. In the views directory, create a new file called form.php.

    <h2>My Form</h2>
    <?php echo form_open('formcontroller/submit'); ?>

    <label for="name">Name:</label>
    <input type="text" name="name" id="name" required>

    <label for="email">Email:</label>
    <input type="email" name="email" id="email" required>

    <input type="submit" value="Submit">

    <?php echo form_close(); ?>

In this code, we use the form_open() function to create an HTML form element. The parameter passed to the function is the URL where our form data will be submitted. The form_close() function indicates the end of the form.

Inside the form, we define our input fields using regular HTML <input> elements. We give each input field a name and optional attributes like id and required. Feel free to add more input fields as needed for your form.

Step 3: Handle Form Submissions

Now that we have our form set up, let's handle the form submissions in the submit() method of our controller.

public function submit() {
    $name = $this->input->post('name');
    $email = $this->input->post('email');

    // Perform validation, database operations, or any desired actions with the form data

    echo "Form submitted successfully!";

In the above code, we retrieve the submitted form data using the input->post() method, passing in the name of the input field as the parameter.

After retrieving the form data, you can perform any necessary validation, database operations, or other actions. For simplicity, we're just echoing a success message.

Step 4: Testing the Form

To test our form, open your web browser and navigate to http://localhost/formcontroller. The form should be displayed, ready for input. Fill in the fields and click the "Submit" button. If everything is set up correctly, you should see the success message.

Congratulations! You've successfully built a form in CodeIgniter. Feel free to enhance your form with additional fields, validation, or customization according to your project requirements.

In conclusion, CodeIgniter provides a straightforward and efficient way to build forms for your web applications. Utilizing the framework's built-in functions and features makes form creation and submission handling a breeze, allowing you to focus on other aspects of your application development. Happy coding!

noob to master © copyleft