I created this tutorial because I was having a hard time finding good simpleCodeIgniter + JQuery tutorial for newbie like me. The one I found, created by MR Forbes, is hard to understand and apparently isn’t working. Another one, pr0digy’s, is using Mootools for the AJAX framework.
So I decided to create my own CodeIgniter and JQuery tutorial based on pr0digy’s tutorial. I’m assuming you already know how to code using CodeIgniter. If you’re new to CodeIgniter, you need to find others tutorial first.The videos given in CodeIgniter’s site is a good start.
This tutorial is about creating simple CodeIgniter + database + ajax system. User will be shown a form to post a message. Then after he/she press the submit button, the system will save the message using ajax and then show the message. You could see the result first if you want.
Database
The first thing we need to do is creating a table to save the message. For this tutorial, We will use this:
Controller
Then, we need to create a controller. My controller is named
message
. You could name your controller any name you like. After that, create 2 functions/methods, view and add.view is used to get the default view and list of messages from the database. The codes are:
We fetch messages from the database using
Message_model->get()
function. Then the data is passed to the view. Here, we have 2 views called. One for the default view, where we called the page using message/view
, and the other is for calling from ajax.add is a proccess used to insert the message to the database. The codes are:
This function is accessed when we press the submit button from the form. This function will save the message using
Message_model->add()
function.Model
The next thing we need to create is the model. I use
Message_model
for the name. Here we create two functions, add and get. add is a function to insert the data into the database. get is a function to retrieve data from database. I think the codes are pretty self-explainatory, but you could drop me a message if you need some explainations on the codes.View
I use 2 files for view section.
default.php
and message_list.php
. Themessage_list
is used for displaying the messages taken from the database.Hey, Where’s the JQuery?
Here we go. The last, and maybe the most important part of this tutorial. So, we the our controller, we had the model, and we had the views. I assume you already know how to include a jquery script to your view. The jquery codes are this:
So, when we click the submit button, the javascript will take the value of input textbox then send it to
message/add
using post method. When the action succeed, the script will call message/view/ajax
. Note the ajax
keyword. It will call the message_list
view instead of the default one. Then the view will replace the content in div#content
tag.Well done. You could see the demo I made from the tutorial.
You could download the whole files used in this tutorial in tutorial.zip (1.98 KB).
How you know whether it is a ajax type or normal in the controller
ReplyDelete