Dynamic Select Options Dependent Dropdown in Codeigniter and Ajax

http://mfikri.com/en/blog/dropdown-ajax-codeigniter

Dynamic Select Options Dependent Dropdown in Codeigniter and Ajax

This is the ultimate tutorial about Select Options dependent using Codeigniter and JQuery Ajax.

If you want to know:

How to create dependent select options, insert, update, and delete with Codeigniter and Ajax.

Then you will like this tutorial.

However, if you are a beginner in Codeigniter I suggest you learn "Codeigniter Tutorial for Beginners" first.

Let’s dive right in.

Table of Content:

1. Preparation

2. Creating a Database and Table.

3. Installing and Configuring Codeigniter

4. Basic Dependent dropdown in Codeigniter and Ajax.

5. Insert with Select Option dependent in Codeigniter and Ajax.

6. Displays data that has been inserted into datatable.

7. Update with Select Option dependent in Codeigniter and Ajax.

8. Delete data to the database.

Step #1. Preparation

To create select options dependent in Codeigniter and AJAX Jquery, here's what you need to prepare:

1. Codeigniter

2. JQuery

3. Bootstrap (optional)

4. Datatable (optional)

Even though Bootstrap and Datatable are optional, I strongly recommend that you have all the lists above.

Because I will use all the lists above in this tutorial.

Step #2. Creating a Database and Table

Create a database named “pos_db”. If you create a database with the same name it's better.

To create a database “pos_db” in MySQL, it can be done by executing the following query:

Next is creating tables. In this tutorial, we need 3 tables, namely: category, sub_category, and product.

To create a "category" table can be done by executing the following query:

To create a "sub_category" table can be done by executing the following query:

To create a "product" table can be done by executing the following query:

Next, insert some data into the "category" and "sub_category" tables.

Insert some data into the "category" table by executing the following query:

The SQL command above will insert 3 records data into a "category" table.

After that, Insert some data into the "sub_category" table by executing the following query:

The SQL command above will insert 9 records data into a "sub_category" table.

Step #3. Installing and Configuring Codeigniter

Extract Codeigniter that you have downloaded earlier to the directory: “C:wampwww” if you use WAMPSERVER.

Or to the directory: “C:xampphtdocs” if you use XAMPP.

Then rename the CodeIgniter project to be “select-ajax”.

Consider the following picture for more details:

Project Name

Next, create a folder "assets" in the "select-ajax" folder parallel to the application and system folder.

Like the picture below:

Assets Folder

After that, extract the Bootstrap file that was previously downloaded into the "assets" folder as shown below:

Bootstrap Files

Next, copy the jquery file into the folder "assets/js" as shown below:

Jquery File

If you haven't got jquery, visit the following URL:

https://code.jquery.com/jquery-3.3.1.js

Select all the code (CTRL + A) and copy then paste it in notepad and save it as jquery-3.3.1.js.

Codeigniter configuration:

Next, configure the following file:

1. Autoload.php

Open the autoload.php file in the "application/config" folder and find the following code:

Then set it to be as follows:

2. Config.php

Open the config.php file in the "application/config" folder and find the following code:

Then set it to be as follows:

Note: if your web server uses a port, include also the port you are using.

3. Database.php

Open the database.php file in the "application/config" folder and find the following code:

Then set it to be as follows:

Step #4. Basic Dependent dropdown in Codeigniter and Ajax

In this step, you will learn the basics of select options dependent using Codeigniter and Ajax.

Let’s begin.

#1. Model

Create a model file named "Product_model.php" in the "application/models" folder.

Then type the following code:

#2. Controller

Create a controller file named "Product.php" in the "application/controllers" folder.

Kemudian ketikan kode berikut:

#3. View

Create a view file named "product_view.php" in the "application/views" folder.

Then type the following code:

#4. Testing

To do the test, open your browser and visit the following URL:

1

http://localhost/select-ajax/index.php/product

Then, it will appear as shown below:

Product Form

Select one option on "Category", then the option will appear from the "Sub Category" that corresponds.

Like the following picture:

Select Option Dependent

In the picture above, I chose the category "Womens Fashion", then the Sub Category will only appear in accordance with the selected category, namely: Clothing, Shoes, and Jewelry.

If you choose a different category, different Sub Category options will also appear.

That is the basic concept of Select Option related in Codeigniter and Ajax.

Step #5. Insert with Select Option dependent in Codeigniter and Ajax

In this step, you will learn how to insert data into a database using select options dependent in Codeigniter and AJAX JQuery.

Let’s dive right in.

#1. Controller

Open the "Product.php" controller in the "application/controllers" folder.

Then change it to be like the code below:

#2. Model

Open the "Product_model.php" model in the "application/models" folder.

Then change it to be like the code below:

#3. View

Create a new view named "add_product_view.php" in the "application/views" folder.

Then type the following code:

#4. Testing

To do the test, open your browser and visit the following URL:

1

http://localhost/select-ajax/index.php/product/add_new

Then, it will appear as shown below:

Add new Product

Then input product name, category, sub category, and product price.

Then click the "Save Product" button to save the product to the database.

If it is running well, then it will look like the following picture:

Product Saved

Step #6. Displays data that has been inserted into datatable

Extract the Datatable file that was previously downloaded into the "assets" folder.

Extract the datatables.css file into the "assets/css" folder and datatables.js into the "assets/js" folder.

Consider the following picture for more details:

Datatable Files

After that, follow the steps below:

#1. Controller

Open the "Product.php" controller in the "application/controllers" folder.

Then change it to be like the code below:

#2. Model

Open the "Product_model.php" model in the "application/models" folder.

Then change it to be like the code below:

#3. View

Create a new view named "product_list_view.php" in the "application/views" folder.

Then type the following code:

#4. Testing

To do the test, open your browser and visit the following URL:

1

http://localhost/select-ajax/index.php/product

Then, it will appear as shown below:

Product List

Click the "Add New Product" button to add a new product.

Add New Product

Input all inputs, then click the "Save Product" button.

If it is running well, it will appear as follows:

Product Saved

Step #7. Update with Select Option dependent in Codeigniter and Ajax

At this step, I will share how to update data with select options dependent on Codeigniter and AJAX.

Let’s dive right in.

First, open the view file "product_list_view.php" which is in the "application/views" folder.

Then find the following code:

1

<a href="" class="btn btn-sm btn-info">Edit</a>

Then change it to be like the code below:

1

<a href="<?php echo site_url('product/get_edit/'.$row->product_id);?>" class="btn btn-sm btn-info">Edit</a>

After that, follow the steps below:

#1. Controller

Open the "Product.php" controller in the "application / controllers" folder.

Then change it to be like this:

#2. Model

Open the "Product_model.php" model in the "application/models" folder.

Then change it to be like this:

#3. View

Create a new view with the name "edit_product_view.php" in the "application/views" folder.

Then type the following code:

#4. Testing

To do the test, open your browser and visit the following URL:

1

http://localhost/select-ajax/index.php/product

Then, it will appear as shown below:

Product List Edit

Then click the “edit” button, then the edit form will appear as shown below:

Edit Form

Edit the data you want to edit, then click the "Update Product" button.

If it is running well, it will look like the following picture:

Product Updated

Step #8. Delete data to the database

At this step, I will share how to delete data to the database.

Let’s begin.

First of all, open the view file "product_list_view.php" in the "application/views" folder.

Then find the following code:

Then change it to be like the following code:

After that, follow the steps below:

#1. Controller

Open the "Product.php" controller in the "application/controllers" folder.

Then add the delete function below:

#2. Model

Open the "Product_model.php" model in the "application/models" folder.

Then add the "delete_product" function below:

#3. Testing

To do the test, open your browser and visit the following URL:

Then click one of the "Delete" buttons to delete the data.

If it is running well, it will look like the following picture:

Product Deleted

Conclusion:

This tutorial is about how to create select options dependent using Codeigniter and AJAX.

Starting from preparation, database creation and tables, Codeigniter installation, Codeigniter configuration, up to CRUD (Create Read Update Delete) with select dependent options using CodeIgniter and Ajax.

So what are you waiting for, Let’s Coding.!http://mfikri.com/en/blog/dropdown-ajax-codeigniter

Last updated

Was this helpful?