Codeigniter4: Howto fetch data by ID (button click) from database using JQuery Ajax

18th Aug 2022 | category: Frameworks | Hits: 3647 Codeigniter4: Howto fetch data by ID (button click) from database using JQuery Ajax

Learning has never stopped. That is why I wanted to share this little piece of code if it could be helpful to another codeigniter lover or developer.

Let's say you want to display products from their particular categories.

So it will start with a point where you have created buttons that show names of categories and every time a user clicks on a particular category button, products will display from it.

Categories

Model
 
                                    
Controller
 
                                    
Views

NOTE: This view file all.php is the same to display also products but in a separate div with id=”products”. Failed to get code for view file here. so i have provided a screenshot

 
                                    

PRODUCTS

Model
 
                                    
Controller
 
                                    

NOTE: “all.php” code is already up. Just ensure you have the same controller file to handle categories and products queries.
The last piece of code needed is this for JQUERY AJAX

JQUERY AJAX (in all.php) view file.
 
                                    

That should be able to help display your product with button click into another defined Div.

NOTE: Ensure jquery is loaded