<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Little Brain &#187; jquery</title>
	<atom:link href="http://justalittlebrain.wordpress.com/tag/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://justalittlebrain.wordpress.com</link>
	<description>just a little brain</description>
	<lastBuildDate>Fri, 27 Jan 2012 09:05:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='justalittlebrain.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Little Brain &#187; jquery</title>
		<link>http://justalittlebrain.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://justalittlebrain.wordpress.com/osd.xml" title="Little Brain" />
	<atom:link rel='hub' href='http://justalittlebrain.wordpress.com/?pushpress=hub'/>
		<item>
		<title>CodeIgniter and Ajax Using jQuery tutorial using JSON</title>
		<link>http://justalittlebrain.wordpress.com/2010/10/24/codeigniter-and-ajax-using-jquery-tutorial-using-json/</link>
		<comments>http://justalittlebrain.wordpress.com/2010/10/24/codeigniter-and-ajax-using-jquery-tutorial-using-json/#comments</comments>
		<pubDate>Sun, 24 Oct 2010 09:09:49 +0000</pubDate>
		<dc:creator>kriwil</dc:creator>
				<category><![CDATA[codeigniter]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[ci]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://littlebrain.org/?p=243</guid>
		<description><![CDATA[This post is outdated, and might and might not work. Unfortunately I currently don&#8217;t have time to check and/or fix it. Sorry So now you have created CodeIgniter + AJAX jQuery, you&#8217;ll want to use JSON, javascript way to exchange data. Assuming you already understand how to do CI + AJAX jQuery, using JSON should [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=justalittlebrain.wordpress.com&amp;blog=31033593&amp;post=243&amp;subd=justalittlebrain&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><strong>This post is outdated, and might and might not work. Unfortunately I currently don&#8217;t have time to check and/or fix it. Sorry <img src='http://s0.wp.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </strong></p>
<p>So now you have created <a href="http://littlebrain.org/2008/05/27/codeigniter-and-ajax-using-jquery-tutorial/">CodeIgniter + AJAX jQuery</a>, you&#8217;ll want to use JSON, javascript way to exchange data.</p>
<p>Assuming you already understand how to do <a href="http://littlebrain.org/2008/05/27/codeigniter-and-ajax-using-jquery-tutorial/">CI + AJAX jQuery</a>, using JSON should be pretty easy. We&#8217;ll only need to extend couple things. First, we&#8217;ll be extending <code>Message::view</code> in <code>Message controller</code> from that tutorial. Then we&#8217;ll extend the view to fetch/process the data.</p>
<h4>Controller</h4>
<p>The previous method was like this:</p>
<pre>function view($type = NULL)
{
    // get data from database
    $data['messages'] = $this-&gt;Message_model-&gt;get();

    if ($type == "ajax") // load inline view for call from ajax
        $this-&gt;load-&gt;view('messages_list', $data);
    else // load the default view
        $this-&gt;load-&gt;view('default', $data);
}</pre>
<p>Since we already handled the view type we want to return, all we have to do is add more handler for json. We&#8217;ll just convert the array of objects we get from model using <a href="http://php.net/json_encode">json_encode</a> function. Here&#8217;s the code:</p>
<pre>function view($type = NULL)
{
    // get data from database
    $data['messages'] = $this-&gt;Message_model-&gt;get();

    if ($type == "ajax") // load inline view for call from ajax
        $this-&gt;load-&gt;view('messages_list', $data);

    else if ($type == 'json') // json, print the output directly
    {
        echo json_encode($data['messages']);
    }

    else // load the default view
        $this-&gt;load-&gt;view('default', $data);
}</pre>
<h4>View</h4>
<p>We&#8217;ll be adding a button to trigger the new &#8220;fetch json data&#8221; so the previous handling is still functional.</p>
<p>The previous view (<code>default.php</code>) was:</p>
<div id="form"></div>
<div id="content">load-&gt;view(&#8216;messages_list&#8217;) ?&gt;</div>
<p>Then we added second submit button:</p>
<div id="form"></div>
<div id="content">load-&gt;view(&#8216;messages_list&#8217;) ?&gt;</div>
<p>Now the javacript. The previous javascript/jquery code was:</p>
<pre>$(document).ready(function() {
    $('#submit').click(function() {

        var msg = $('#message').val();

        $.post("", {message: msg}, function() {
            $('#content').load("");
            $('#message').val('');
        });
    });
});</pre>
<p>Then we need to create handler to fetch the data (in json) and put it in correct html tag. Here&#8217;s the code after we added handler for second submit button:</p>
<pre>$(document).ready(function() {
    $('#submit').click(function() {

        var msg = $('#message').val();

        $.post("", {message: msg}, function() {
            $('#content').load("");
            $('#message').val('');
        });
    });

    $('#submit2').click(function() {
        var msg = $('#message').val();
        $('#message').val('');
        $.post("", {message: msg}, function() {
            $.getJSON("", function(data) {
                var contentHtml = "</pre>
<ol>&#8220;; $(data).each(function(index, item) { contentHtml += &#8220;</p>
<li>&#8220;; contentHtml += item.message; contentHtml += &#8220;</li>
<p>&#8220;; }); contentHtml += &#8220;</ol>
<pre>
";
                $('#content').html(contentHtml);
            });
        });
    });
});</pre>
<p>It&#8217;s done. <a href="http://sandbox.littlebrain.org/ci/index.php/message/view">Try yourself</a>, or <a href="http://dl.dropbox.com/u/112837/littlebrain.org/243/applications.zip">download the files</a> needed.</p>
<p>I know, it&#8217;s pretty basic. But I think it&#8217;s enough to get you started to use json in exchanging data. Why json instead of complete html? less data transfer.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/justalittlebrain.wordpress.com/243/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/justalittlebrain.wordpress.com/243/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/justalittlebrain.wordpress.com/243/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/justalittlebrain.wordpress.com/243/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/justalittlebrain.wordpress.com/243/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/justalittlebrain.wordpress.com/243/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/justalittlebrain.wordpress.com/243/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/justalittlebrain.wordpress.com/243/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/justalittlebrain.wordpress.com/243/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/justalittlebrain.wordpress.com/243/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/justalittlebrain.wordpress.com/243/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/justalittlebrain.wordpress.com/243/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/justalittlebrain.wordpress.com/243/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/justalittlebrain.wordpress.com/243/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=justalittlebrain.wordpress.com&amp;blog=31033593&amp;post=243&amp;subd=justalittlebrain&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://justalittlebrain.wordpress.com/2010/10/24/codeigniter-and-ajax-using-jquery-tutorial-using-json/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/17b185ab2d1b7dcad3f488040f76385c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">kriwil</media:title>
		</media:content>
	</item>
		<item>
		<title>CodeIgniter and Ajax Using JQuery Tutorial</title>
		<link>http://justalittlebrain.wordpress.com/2008/05/27/codeigniter-and-ajax-using-jquery-tutorial/</link>
		<comments>http://justalittlebrain.wordpress.com/2008/05/27/codeigniter-and-ajax-using-jquery-tutorial/#comments</comments>
		<pubDate>Tue, 27 May 2008 15:28:59 +0000</pubDate>
		<dc:creator>kriwil</dc:creator>
				<category><![CDATA[howto]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[ci]]></category>
		<category><![CDATA[codeigniter]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://littlebrain.org/?p=172</guid>
		<description><![CDATA[This post is outdated, and might and might not work. Unfortunately I currently don&#8217;t have time to check and/or fix it. Sorry I created this tutorial because I was having a hard time finding good simple CodeIgniter + JQuery tutorial for newbie like me. The one I found, created by MR Forbes, is hard to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=justalittlebrain.wordpress.com&amp;blog=31033593&amp;post=172&amp;subd=justalittlebrain&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><strong>This post is outdated, and might and might not work. Unfortunately I currently don&#8217;t have time to check and/or fix it. Sorry <img src='http://s0.wp.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </strong></p>
<p>I created this tutorial because I was having a hard time finding good simple <a href="http://codeigniter.com/">CodeIgniter</a> + <a href="http://jquery.com/">JQuery</a> tutorial for newbie like me. <a href="http://www.mrforbes.com/wordpress/2007/05/13/a-quick-code-igniter-and-jquery-ajax-tutorial/">The one I found</a>, created by MR Forbes, is hard to understand and apparently isn&#8217;t working. <a href="http://pr0digy.com/codeigniter/unobtrusive-ajax-with-codeigniter-and-mootools/">Another one</a>, pr0digy&#8217;s, is using Mootools for the AJAX framework.</p>
<p>So I decided to create my own CodeIgniter and JQuery tutorial based on pr0digy&#8217;s tutorial. I&#8217;m assuming you already know how to code using CodeIgniter. If you&#8217;re new to CodeIgniter, you need to find others tutorial first. <a href="http://codeigniter.com/tutorials/">The videos</a> given in CodeIgniter&#8217;s site is a good start.</p>
<p>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 <a href="http://sandbox.littlebrain.org/ci/index.php/message/view">the result</a> first if you want.</p>
<h4>Database</h4>
<p>The first thing we need to do is creating a table to save the message. For this tutorial, We will use this:</p>
<pre>CREATE TABLE IF NOT EXISTS `messages` (
  `id` tinyint(4) NOT NULL auto_increment,
  `message` varchar(256) NOT NULL,
  PRIMARY KEY  (`id`)
)</pre>
<h4>Controller</h4>
<p>Then, we need to create a controller. My controller is named <code>message</code>. You could name your controller any name you like. After that, create 2 functions/methods, <strong>view</strong> and <strong>add</strong>.</p>
<p>view is used to get the default view and list of messages from the database. The codes are:</p>
<pre>function view($type = NULL)
{
    // get data from database
    $data['messages'] = $this-&gt;Message_model-&gt;get();

    if ($type == "ajax") // load inline view for call from ajax
        $this-&gt;load-&gt;view('messages_list', $data);
    else // load the default view
        $this-&gt;load-&gt;view('default', $data);
}</pre>
<p>We fetch messages from the database using <code>Message_model-&gt;get()</code> 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 <code>message/view</code>, and the other is for calling from ajax.</p>
<p>add is a proccess used to insert the message to the database. The codes are:</p>
<pre>function add()
{
    // if HTTP POST is sent, add the data to database
    if($_POST &amp;&amp; $_POST['message'] != NULL) {
        $message['message'] = $this-&gt;input-&gt;xss_clean($_POST['message']);
        $this-&gt;Message_model-&gt;add($message);
    } else
        redirect('message/view');
}</pre>
<p>This function is accessed when we press the submit button from the form. This function will save the message using <code>Message_model-&gt;add()</code> function.</p>
<h4>Model</h4>
<p>The next thing we need to create is the model. I use <code>Message_model</code> for the name. Here we create two functions, <strong>add</strong> and <strong>get</strong>. 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.</p>
<pre>function add($data)
{
    $this-&gt;db-&gt;insert('messages', $data);
}

function get($limit=5, $offset=0)
{
    $this-&gt;db-&gt;orderby('id', 'DESC');
    $this-&gt;db-&gt;limit($limit, $offset);

    return $this-&gt;db-&gt;get('messages')-&gt;result();
}</pre>
<h4>View</h4>
<p>I use 2 files for view section. <code>default.php</code> and <code>message_list.php</code>. The <code>message_list</code> is used for displaying the messages taken from the database.</p>
<div id="form"></div>
<div id="content">load-&gt;view(&#8216;messages_list&#8217;) ?&gt;</div>
<ol>
<li>message ?&gt;</li>
</ol>
<h4>Hey, Where&#8217;s the JQuery?</h4>
<p>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:</p>
<pre>$(document).ready(function() {
    $('#submit').click(function() {

        var msg = $('#message').val();

        $.post("", {message: msg}, function() {
            $('#content').load("");
            $('#message').val('');
        });
    });
});</pre>
<p>So, when we click the submit button, the javascript will take the value of input textbox then send it to <code>message/add</code> using post method. When the action succeed, the script will call <code>message/view/ajax</code>. Note the <code>ajax</code> keyword. It will call the <code>message_list</code> view instead of the default one. Then the view will replace the content in <code>div#content</code> tag.</p>
<p>Well done. You could see <a href="http://sandbox.littlebrain.org/ci/index.php/message/view">the demo I made from the tutorial</a>.</p>
<p>You could download the whole files used in this tutorial in [download#1#size#nohits].</p>
<p>Or, if you prefer to get one by one,<br />
[download#2#size#nohits]<br />
[download#3#size#nohits]<br />
[download#4#size#nohits]<br />
[download#5#size#nohits]</p>
<p>I hope this tutorial helps newbie like me. If you have any question, just drop a comment below <img src='http://s1.wp.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p><em><strong>Update</strong>: Need how to do this using JSON? Check <a href="http://littlebrain.org/2010/10/24/codeigniter-and-ajax-using-jquery-tutorial-using-json/">CodeIgniter and Ajax Using jQuery tutorial using JSON</a>.</em></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/justalittlebrain.wordpress.com/172/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/justalittlebrain.wordpress.com/172/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/justalittlebrain.wordpress.com/172/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/justalittlebrain.wordpress.com/172/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/justalittlebrain.wordpress.com/172/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/justalittlebrain.wordpress.com/172/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/justalittlebrain.wordpress.com/172/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/justalittlebrain.wordpress.com/172/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/justalittlebrain.wordpress.com/172/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/justalittlebrain.wordpress.com/172/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/justalittlebrain.wordpress.com/172/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/justalittlebrain.wordpress.com/172/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/justalittlebrain.wordpress.com/172/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/justalittlebrain.wordpress.com/172/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/justalittlebrain.wordpress.com/172/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/justalittlebrain.wordpress.com/172/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=justalittlebrain.wordpress.com&amp;blog=31033593&amp;post=172&amp;subd=justalittlebrain&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://justalittlebrain.wordpress.com/2008/05/27/codeigniter-and-ajax-using-jquery-tutorial/feed/</wfw:commentRss>
		<slash:comments>149</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/17b185ab2d1b7dcad3f488040f76385c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">kriwil</media:title>
		</media:content>
	</item>
	</channel>
</rss>
