<?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/"
	>

<channel>
	<title>Emanuele Feronato &#187; Ajax</title>
	<atom:link href="http://www.emanueleferonato.com/category/ajax/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.emanueleferonato.com</link>
	<description>italian geek and PROgrammer</description>
	<lastBuildDate>Thu, 02 Sep 2010 09:32:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
		<item>
		<title>Loading WP posts with Ajax and jQuery &#8211; a real world example</title>
		<link>http://www.emanueleferonato.com/2010/04/06/loading-wordpress-posts-with-ajax-and-jquery-a-real-world-example/</link>
		<comments>http://www.emanueleferonato.com/2010/04/06/loading-wordpress-posts-with-ajax-and-jquery-a-real-world-example/#comments</comments>
		<pubDate>Tue, 06 Apr 2010 21:28:26 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Php]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=2677</guid>
		<description><![CDATA[If you are looking for a real world example made with Loading WordPress posts with Ajax and jQuery or you haven&#8217;t played LineBall yet, or you played it, you loved it and you want to know who made the music, this is the right blog post. I am finishing and polishing LineBall&#8217;s official site built [...]]]></description>
			<content:encoded><![CDATA[<p>If you are looking for a real world example made with <a href="http://www.emanueleferonato.com/2010/04/01/loading-wordpress-posts-with-ajax-and-jquery/">Loading WordPress posts with Ajax and jQuery</a> or you haven&#8217;t played <a href="http://www.emanueleferonato.com/2010/03/29/lineball-flash-game-sponsored-and-released/">LineBall</a> yet, or you played it, you loved it and you want to know who made the music, this is the right blog post.</p>
<p>I am finishing and polishing <a href="http://www.lineball-game.com/" target = "_blank">LineBall&#8217;s official site</a> built with WordPress and Ajax.</p>
<p>I plan to finish it before this week ends, then release the theme next week. So if you have suggestions, feedback or want more information, this is your turn.</p>
<p>Help me to make the ultimate WordPress Flash game theme.</p>
<p>Some features already developed:</p>
<p>* Highly customizable by setup page &#8211; no need to edit the code</p>
<p>* Ajax powered to browse it without reloading the header (the game itself)</p>
<p>* Minimal theme, a very few lines of code</p>
<p>Let me know what do you think about it. And don&#8217;t pay that much attention to contents&#8230; I just wrote a couple of lines just to make some tests</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2010/04/06/loading-wordpress-posts-with-ajax-and-jquery-a-real-world-example/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Loading WordPress posts with Ajax and jQuery</title>
		<link>http://www.emanueleferonato.com/2010/04/01/loading-wordpress-posts-with-ajax-and-jquery/</link>
		<comments>http://www.emanueleferonato.com/2010/04/01/loading-wordpress-posts-with-ajax-and-jquery/#comments</comments>
		<pubDate>Thu, 01 Apr 2010 21:52:41 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Php]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=2650</guid>
		<description><![CDATA[After seeing how to include jQuery Ajax calls in your WordPress blog, it&#8217;s time to load posts on the fly, without reloading the page. As for the previous example I am using the standard Kubrick theme&#8230; without any plugin installed. Look how I load the posts under the header by clicking on their titles&#8230; even [...]]]></description>
			<content:encoded><![CDATA[<p>After seeing <a href="http://www.emanueleferonato.com/2010/03/30/including-jquery-ajax-calls-in-your-wordpress-blog/">how to include jQuery Ajax calls in your WordPress blog</a>, it&#8217;s time to load posts on the fly, without reloading the page.</p>
<p>As for the previous example I am using the standard Kubrick theme&#8230; without any plugin installed.</p>
<p>Look how I load the posts under the header by clicking on their titles&#8230; even the ones with &#8220;more&#8221; tag appear complete without reloading the page:</p>
<p><object width="520" height="417"><param name="movie" value="http://www.youtube.com/v/_q7tKTn_z9s&#038;hl=it_IT&#038;fs=1&#038;rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/_q7tKTn_z9s&#038;hl=it_IT&#038;fs=1&#038;rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="520" height="417"></embed></object></p>
<p>Let&#8217;s start with <code>header.php</code> modifications</p>
<p>This is the script I added under</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> wp_head<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>That is the JQuery part<span id="more-2650"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">&lt;script type=&quot;text/javascript&quot; src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js&quot;&gt;&lt;/script&gt;
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
	$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	$.<span style="color: #660066;">ajaxSetup</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>cache<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;h2 a&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> post_id <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;rel&quot;</span><span style="color: #009900;">&#41;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#your_post_here&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;loading...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#your_post_here&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">load</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;http://&lt;?php echo $_SERVER[HTTP_HOST]; ?&gt;/triqui-ajax/&quot;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span>id<span style="color: #339933;">:</span>post_id<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>I already explained the first 4 lines in the <a href="http://www.emanueleferonato.com/2010/03/30/including-jquery-ajax-calls-in-your-wordpress-blog/">previous tutorial</a>.</p>
<p><strong>Line 5</strong>: Waiting for the user to click on an hypertext over an <code>h2</code> heading. I am doing it because in Kubrick post titles are rendered this way:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;h2&gt;&lt;a href=&quot;&lt;?php the_permalink() ?&gt;&quot;....</pre></div></div>

<p>This should change from theme to theme, but this works on Kubrick</p>
<p><strong>Line 6</strong>: Retrieving the <code>rel</code> attribute of the link and saving it in a variable called <code>post_id</code>. You&#8217;ll see later in this tutorial how this attribute will contain the unique ID of the post we want to show.</p>
<p><strong>Line 7</strong>: In an element with <code>your_post_here</code> id I am writing &#8220;loading&#8230;&#8221; because I&#8217;m starting to load the post. You&#8217;ll see later in this tutorial where to place the element</p>
<p><strong>Line 8</strong>: Now thw ajax magic&#8230; in the <code>your_post_here</code> element this time I load the output of a page of the blog called <code>triqui-ajax</code>. You&#8217;ll se later in this tutorial how to create it. This will work if the permalinks of your blog aren&#8217;t the default ones. Go to <code>Settings</code> -> <code>Permalinks</code> and select <code>Day and name</code></p>
<p><img src="/wp-content/uploads/2010/04/wp_ajax_2.jpg" alt="" /></p>
<p>I am also passing in <code>POST</code> a variable called <code>id</code> with the content of <code>post_id</code> variable (the post unique id)</p>
<p><strong>Line 9</strong>: I added this line to prevent the browser to jump to the link&#8230; remember? I want to load the post in the same page.</p>
<p>Now it&#8217;s time to make some changes to <code>index.php</code></p>
<p>This is how I modified it:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #009933; font-style: italic;">/**
 * @package WordPress
 * @subpackage Default_Theme
 */</span>
&nbsp;
get_header<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
	&lt;div id=&quot;content&quot; class=&quot;narrowcolumn&quot; role=&quot;main&quot;&gt;
	&lt;div id = &quot;your_post_here&quot;&gt;&lt;/div&gt;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>have_posts<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>have_posts<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> the_post<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
			&lt;div <span style="color: #000000; font-weight: bold;">&lt;?php</span> post_class<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> id=&quot;post-<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_ID<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;
				&lt;h2&gt;&lt;a href=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_permalink<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot; rel=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_ID<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot; title=&quot;Permanent Link to <span style="color: #000000; font-weight: bold;">&lt;?php</span> the_title_attribute<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_title<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/a&gt;&lt;/h2&gt;
				&lt;small&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_time<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'F jS, Y'</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> &lt;!-- by <span style="color: #000000; font-weight: bold;">&lt;?php</span> the_author<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> --&gt;&lt;/small&gt;
&nbsp;
				&lt;div class=&quot;entry&quot;&gt; 
					<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_content<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Read the rest of this entry &amp;raquo;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
				&lt;/div&gt;
&nbsp;
				&lt;p class=&quot;postmetadata&quot;&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_tags<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Tags: '</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">', '</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&lt;br /&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> Posted in <span style="color: #000000; font-weight: bold;">&lt;?php</span> the_category<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">', '</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> | <span style="color: #000000; font-weight: bold;">&lt;?php</span> edit_post_link<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Edit'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">' | '</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>  <span style="color: #000000; font-weight: bold;">&lt;?php</span> comments_popup_link<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'No Comments &amp;#187;'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'1 Comment &amp;#187;'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'% Comments &amp;#187;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/p&gt;
			&lt;/div&gt;
&nbsp;
		<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endwhile</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
		&lt;div class=&quot;navigation&quot;&gt;
			&lt;div class=&quot;alignleft&quot;&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> next_posts_link<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&amp;laquo; Older Entries'</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/div&gt;
			&lt;div class=&quot;alignright&quot;&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> previous_posts_link<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Newer Entries &amp;raquo;'</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/div&gt;
		&lt;/div&gt;
&nbsp;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">else</span> <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
		&lt;h2 class=&quot;center&quot;&gt;Not Found&lt;/h2&gt;
		&lt;p class=&quot;center&quot;&gt;Sorry, but you are looking for something that isn't here.&lt;/p&gt;
		<span style="color: #000000; font-weight: bold;">&lt;?php</span> get_search_form<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
	&lt;/div&gt;
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> get_sidebar<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> get_footer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>To tell the truth, I only added a line and changed a bit another one:</p>
<p><strong>Line 10</strong>: This is the div that will contain the post, the one I change with jQuery (remember? <strong>lines 7</strong> and <strong>8</strong> in the jQuery script)</p>
<p><strong>Line 16</strong>: Here I changed the <code>rel</code> attribute to store the unique id of the post I want to load. I used it at <strong>line 6</strong> in the jQuery script</p>
<p>And the template files do not need any other change.</p>
<p>Now it&#8217;s time to create a new page template with some code that will load the selected post.</p>
<p>In your template directory create a new php file, no matter its name&#8230; you can call it <code>example.php</code> and write this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">/*
Template Name: Triqui Ajax Post
*/</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
	<span style="color: #000088;">$post</span> <span style="color: #339933;">=</span> get_post<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
	<span style="color: #000000; font-weight: bold;">&lt;?php</span> setup_postdata<span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
	&lt;div <span style="color: #000000; font-weight: bold;">&lt;?php</span> post_class<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> id=&quot;post-<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_ID<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;
		&lt;h2&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_title<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/h2&gt;
		&lt;small&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_time<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'F jS, Y'</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> &lt;!-- by <span style="color: #000000; font-weight: bold;">&lt;?php</span> the_author<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> --&gt;&lt;/small&gt;
&nbsp;
		&lt;div class=&quot;entry&quot;&gt; 
			<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_content<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Read the rest of this entry &amp;raquo;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
		&lt;/div&gt;
&nbsp;
		&lt;p class=&quot;postmetadata&quot;&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_tags<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Tags: '</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">', '</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&lt;br /&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> Posted in <span style="color: #000000; font-weight: bold;">&lt;?php</span> the_category<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">', '</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> | <span style="color: #000000; font-weight: bold;">&lt;?php</span> edit_post_link<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Edit'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">' | '</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>  <span style="color: #000000; font-weight: bold;">&lt;?php</span> comments_popup_link<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'No Comments &amp;#187;'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'1 Comment &amp;#187;'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'% Comments &amp;#187;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/p&gt;
	&lt;/div&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>As you can see it&#8217;s almost the same code you can find in <code>index.php</code> at <strong>lines 15-24</strong>&#8230; because I just want to load a post in the same way the index file does. I just removed the anchor tag in the title, because I don&#8217;t want it to be clickable. And obviously I read the <code>id</code> value passed by the jQuery script at <strong>line 8</strong></p>
<p>The only interesting lines are <strong>lines 2-4</strong>&#8230; yes, the comment&#8230; because this way I am giving the template page the name <code>Triqui Ajax Post</code>.</p>
<p>Now in your admin area create a new page, call it <code>Triqui Ajax</code> (do you remember the permalink url at <strong>line 8</strong> in the jQuery script&#8230;) and select <code>Triqui Ajax Post</code> as template</p>
<p><img src="/wp-content/uploads/2010/04/wp_ajax.jpg" alt="" /></p>
<p>And that&#8217;s it&#8230; you don&#8217;t need anything else, and the blog will work as in the video.</p>
<p>Enjoy</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2010/04/01/loading-wordpress-posts-with-ajax-and-jquery/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Including jQuery Ajax calls in your WordPress blog</title>
		<link>http://www.emanueleferonato.com/2010/03/30/including-jquery-ajax-calls-in-your-wordpress-blog/</link>
		<comments>http://www.emanueleferonato.com/2010/03/30/including-jquery-ajax-calls-in-your-wordpress-blog/#comments</comments>
		<pubDate>Tue, 30 Mar 2010 15:33:46 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Php]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=2637</guid>
		<description><![CDATA[I want to show you how to include jQuery ajax calls in your WordPress blog. The reason is simple: sometimes you may need to update the content of a WP page without reloading the entire page. This is useful when you want the user to interact with your blog without forcing him to reload the [...]]]></description>
			<content:encoded><![CDATA[<p>I want to show you how to include jQuery ajax calls in your WordPress blog.</p>
<p>The reason is simple: sometimes you may need to update the content of a WP page without reloading the entire page.</p>
<p>This is useful when you want the user to interact with your blog without forcing him to reload the page. Or when you want some events to happen &#8220;automatically&#8221; without user interaction.</p>
<p>In this video I am changing the header at every second without reloading the page.</p>
<p><object width="520" height="420"><param name="movie" value="http://www.youtube.com/v/DbEC5qjlS8E&#038;hl=it&#038;fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/DbEC5qjlS8E&#038;hl=it&#038;fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="520" height="420"></embed></object></p>
<p>Also notice I am doing it from the default Kubrick theme with a fresh installation&#8230; I am not using any jQuery powered theme or plugin. Let&#8217;s see how can you make it.<span id="more-2637"></span></p>
<p>In this case all changes have been made in <code>header.php</code> file, adding nine lines after</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> wp_head<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Here they are:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">&lt;script type=&quot;text/javascript&quot; src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js&quot;&gt;&lt;/script&gt;
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
	$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	$.<span style="color: #660066;">ajaxSetup</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>cache<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> refresh <span style="color: #339933;">=</span> setInterval<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>   	
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#description_span&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">load</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&lt;?php bloginfo('template_url'); ?&gt;/example.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

<p>First, I am loading jQuery using Google as content delivery network.</p>
<p>Then, when the document is ready, I disable the cache to stop jQuery <code>.load</code> response from being cached.</p>
<p>Now I just execute an ajax call to <code>example.php</code> file at every second and print the result in the element with <code>description_span</code> id</p>
<p>This is <code>example.php</code> content</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">10000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Now it&#8217;s time to locate <code>description_span</code> element&#8230; it&#8217;s in the header, next to the description of the blog.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;div class=&quot;description&quot;&gt;&lt;?php bloginfo('description'); ?&gt; like other &lt;span id = &quot;description_span&quot;&gt;&lt;/span&gt; ones&lt;/div&gt;</pre></div></div>

<p>And that&#8217;s it. This time I changed the header without touching the content, next time (the aim of my experiment) I&#8217;ll change the content without touching the header, maybe because the header contains a Flash game called <a href="http://www.emanueleferonato.com/2010/03/29/lineball-flash-game-sponsored-and-released/">LineBall</a>&#8230; (yes, I am designing a WP theme for a game official site).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2010/03/30/including-jquery-ajax-calls-in-your-wordpress-blog/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Creation of a sortable list with Ajax tutorial</title>
		<link>http://www.emanueleferonato.com/2008/04/21/creation-of-a-sortable-list-with-ajax-tutorial/</link>
		<comments>http://www.emanueleferonato.com/2008/04/21/creation-of-a-sortable-list-with-ajax-tutorial/#comments</comments>
		<pubDate>Mon, 21 Apr 2008 20:44:55 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Php]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=258</guid>
		<description><![CDATA[One interesting Web 2.0 effect is the sortable list, and we are going to make one in less than five minutes thanks to Prototype and script.aculo.us libraries. Prototype is a JavaScript Framework that aims to ease development of dynamic web applications and can be downloaded here, while script.aculo.us (scriptaculous from now on) provides you with [...]]]></description>
			<content:encoded><![CDATA[<p>One interesting Web 2.0 effect is the sortable list, and we are going to make one in less than five minutes thanks to <a target = "_blank" href="http://www.prototypejs.org/">Prototype</a> and <a target = "_blank" href="http://script.aculo.us/">script.aculo.us</a> libraries.</p>
<p><strong>Prototype</strong> is a JavaScript Framework that aims to ease development of dynamic web applications and can be downloaded <a target = "_blank" href="http://www.prototypejs.org/download">here</a>, while <strong>script.aculo.us</strong> (scriptaculous from now on) provides you with easy-to-use, cross-browser user interface JavaScript libraries to make your web sites and web applications fly and can be downloaded <a target = "_blank" href="http://script.aculo.us/downloads">here</a>.</p>
<p>This tutorial is based on zen of shen&#8217;s pseudocode found at <a target = "_blank" href="http://www.zenofshen.com/2008/01/08/scriptaculous-ajax-sortable-lists-tutorial/">script.aculo.us Ajax Sortable Lists Tutorial</a>.</p>
<p>This tutorial will show real code and a step by step guide through MySql, php and Ajax coding in order to create your sortable list.</p>
<p>Of course your hosting plan must allow you to use php and MySql, so refer to your hosting provider if you don&#8217;t know if your web space supports those features.</p>
<p><strong>MYSQL</strong></p>
<p>First we need to work on the database to create the table that will store our list. I made the simplest table ever, because it&#8217;s just an example, but you can add as many columns as you want, in order to fit the table to your needs.</p>
<p>My table only has three fields: the primary key <code>id</code>, the name of the list element <code>color</code> and the core column, the one where you will save the order of the elements, called, in a lack of creativity, <code>color_order</code>.<span id="more-258"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">`sortable`</span> <span style="color: #66cc66;">&#40;</span>
<span style="color: #ff0000;">`id`</span> INT <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span> <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">,</span>
<span style="color: #ff0000;">`color`</span> TEXT <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #66cc66;">,</span>
<span style="color: #ff0000;">`color_order`</span> INT <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> 
<span style="color: #66cc66;">&#41;</span> ENGINE <span style="color: #66cc66;">=</span> MYISAM ;</pre></td></tr></table></div>

<p>Once we have the table, let&#8217;s populate it. I am doing it directly with MySql, but again you are free to code your own application to populate it</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> <span style="color: #ff0000;">`sortable`</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">`color`</span> <span style="color: #66cc66;">&#41;</span> 
<span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span>
<span style="color: #ff0000;">'Red'</span>
<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">&#40;</span>
<span style="color: #ff0000;">'Yellow'</span>
<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">&#40;</span>
<span style="color: #ff0000;">'Green'</span>
<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">&#40;</span>
<span style="color: #ff0000;">'Blue'</span>
<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">&#40;</span>
<span style="color: #ff0000;">'Black'</span>
<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">&#40;</span>
<span style="color: #ff0000;">'White'</span>
<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">&#40;</span>
<span style="color: #ff0000;">'Orange'</span>
<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">&#40;</span>
<span style="color: #ff0000;">'Purple'</span>
<span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>Now the table looks like this one</p>
<p><img src="/images/colors.png" alt="" /></p>
<p>and our MySql task is completed. Now, let&#8217;s write some php and html</p>
<p><strong>PHP/HTML</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000088;">$connection</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span> <span style="color: #009900;">&#40;</span>localhost<span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;your_login&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;your_password&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;your_db&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$connection</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;no db found&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;select * from sortable order by color_order asc&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span>or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$number</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_num_rows</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span><span style="color: #000088;">$x</span><span style="color: #339933;">&lt;=</span><span style="color: #000088;">$number</span><span style="color: #339933;">;</span><span style="color: #000088;">$x</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$list</span><span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;			&lt;li id=<span style="color: #000099; font-weight: bold;">\&quot;</span>item_<span style="color: #006699; font-weight: bold;">$row[id]</span><span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;<span style="color: #006699; font-weight: bold;">$row[color]</span>&lt;/li&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;&gt;
&lt;html&gt;
	&lt;head&gt;
		&lt;script type=&quot;text/javascript&quot; src=&quot;prototype-1.6.0.2.js&quot;&gt;&lt;/script&gt;
		&lt;script type=&quot;text/javascript&quot; src=&quot;scriptaculous/src/scriptaculous.js&quot;&gt;&lt;/script&gt;
		&lt;style type = &quot;text/css&quot;&gt;
			ul#sortlist{list-style:none;padding:0px;margin:0px}
			ul#sortlist li{padding:5px;border:1px solid #666666;margin:1px;font:normal 10px verdana;width:200px;color:#666666;background-color:#f0f0f0;cursor:move}
			#output{font:bold 10px verdana}
		&lt;/style&gt;
	&lt;/head&gt;
	&lt;body&gt;
		&lt;ul id = &quot;sortlist&quot;&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$list</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
		&lt;/ul&gt;
		&lt;div id = &quot;output&quot;&gt;Drag to sort your favourite colors&lt;/div&gt;
		&lt;script type=&quot;text/javascript&quot;&gt;
			Sortable.create('sortlist',{
				onUpdate:function(){
					new Ajax.Updater('output','sort.php',{onComplete:function(request){}, parameters:Sortable.serialize('sortlist'), evalScripts:true, asynchronous:true})
				}
			})	
		&lt;/script&gt;
	&lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

<p><strong>Lines 3-4</strong>: Connecting to the database. If you don&#8217;t know your username, password or database name, ask them to your hosting provider</p>
<p><strong>Lines 6-8</strong>: Query to get all the colors ordered by their color order</p>
<p><strong>Lines 10-13</strong>: Building the list from the result of the query just executed</p>
<p><strong>Lines 19-20</strong>: Including Prototype and scriptaculous sources. Remember to include Prototype before Scriptaculous and also remember to change the path if you placed the scripts in a different location</p>
<p><strong>Lines 21-25</strong>: Giving the list a touch of style</p>
<p><strong>Lines 28-30</strong>: Displaying the list</p>
<p><strong>Line 31</strong>: Creation of the <code>div</code> element to display Ajax output</p>
<p><strong>Lines 32-38</strong>: Calling the <code>Sortable.create</code> function to initialize the sortable list. You can find more information about <code>Sortable.create</code> function in the scriptaculous wiki at <a target = "_blank" href="http://wiki.script.aculo.us/scriptaculous/show/Sortable.create">this link</a></p>
<p>And the php/html is over.</p>
<p>Now, I should made the Ajax part. But it&#8217;s already made by Prototype thanks to the <code>Ajax.Updater</code> object, so I just have to make the <code>sort.php</code> script called in the <code>Ajax.Updater</code> object</p>
<p>You can find more information about the Ajax.Updater object in the API Docs pages at <a target = "_blank" href="http://www.prototypejs.org/api/ajax/updater">this link</a></p>
<p>So it&#8217;s basically php again, but I will call this part the Ajax part</p>
<p><strong>AJAX</strong></p>
<p>Create a file called <code>sort.php</code> and write:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000088;">$connection</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;62.149.150.49&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Sql92833&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;0015025a&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$db</span><span style="color: #339933;">=</span> <span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Sql92833_5&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$connection</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span>sortlist<span style="color: #009900;">&#93;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$varname</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$varvalue</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;update sortable set color_order = &quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$varname</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; where id = &quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$varvalue</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;You just updated it on &quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Y-m-d H:i:s&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><strong>Lines 3-4</strong>: Connecting to the database, just like in the php/html part</p>
<p><strong>Lines 6-9</strong>: Scanning all the <code>sortlist</code> array passed with <code>POST</code> method by the <code>Sortable.create</code> and the <code>Ajax.Updater</code> and updating the <code>color_order</code> column of the <code>sortable</code> table according to <code>sortlist</code> names and values</p>
<p><strong>Line 11</strong>: displaying an output message.</p>
<p>And that&#8217;s it! Drag colors as you want, then <a target = "iframe" href="/stuff/sortable/index.php">reload</a> the page in the iframe and the colors will be in the same position you just ordered&#8230; unless someone else changed their order while the page was reloading</p>
<p><iframe name = "iframe" id = "iframe" width = "500" height = "300" src = "/stuff/sortable/index.php"></iframe></p>
<p>That&#8217;s all&#8230; hope you will find it useful as I did.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2008/04/21/creation-of-a-sortable-list-with-ajax-tutorial/feed/</wfw:commentRss>
		<slash:comments>26</slash:comments>
		</item>
	</channel>
</rss>
