<?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>Phillip Napieralski &#187; AJAX</title>
	<atom:link href="http://blog.pnapieralski.com/category/ajax/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.pnapieralski.com</link>
	<description>Programmer, Engineer, Researcher.</description>
	<lastBuildDate>Tue, 20 Dec 2011 16:41:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Inline AJAX Editing</title>
		<link>http://blog.pnapieralski.com/ajax/inline-ajax-editing/</link>
		<comments>http://blog.pnapieralski.com/ajax/inline-ajax-editing/#comments</comments>
		<pubDate>Sat, 23 Jan 2010 16:26:21 +0000</pubDate>
		<dc:creator>Phillip Napieralski</dc:creator>
				<category><![CDATA[AJAX]]></category>
		<category><![CDATA[Happy Polack]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://blog.pnapieralski.com/uncategorized/6/</guid>
		<description><![CDATA[Happypolack.com is a joke site I created as a side project a little bit ago and it's continually being enhanced. The latest feature I added was a way for admins to edit any joke (eg, fix typos), quickly and easily.

This weirdo typed "Your Mommy" instead of "Yo Mama" on one of the jokes. How distracting! Let's write some code so we can fix this on the fly...]]></description>
			<content:encoded><![CDATA[<p>Happypolack.com is a joke site I created as a side project a little bit ago and it&#8217;s continually being enhanced. The latest feature I added was a way for admins to edit any joke (eg, fix typos), quickly and easily.</p>
<h2>An Example</h2>
<p>This weirdo typed &#8220;Your Mommy&#8221; instead of &#8220;Yo Mama.&#8221; How distracting! Let&#8217;s write some code so we can fix this.</p>
<div id="attachment_21" class="wp-caption alignnone" style="width: 464px"><a href="http://blog.pnapieralski.com/wp-content/uploads/2010/01/edit1.png" rel="shadowbox[sbpost-6];player=img;"><img class="size-full wp-image-21    " title="edit1" src="http://blog.pnapieralski.com/wp-content/uploads/2010/01/edit1.png" alt="" width="454" height="91" /></a><p class="wp-caption-text"> </p></div>
<p>If we click the edit link corresponding to the joke, we get something that looks like the following:</p>
<div id="attachment_22" class="wp-caption alignnone" style="width: 463px"><a href="http://blog.pnapieralski.com/wp-content/uploads/2010/01/edit2.png" rel="shadowbox[sbpost-6];player=img;"><img class="size-full wp-image-22   " title="edit2" src="http://blog.pnapieralski.com/wp-content/uploads/2010/01/edit2.png" alt="" width="453" height="91" /></a><p class="wp-caption-text"> </p></div>
<p>Great! We edited it, now we simply click submit and we can go on with reading jokes and being merry!</p>
<div id="attachment_25" class="wp-caption alignnone" style="width: 464px"><a href="http://blog.pnapieralski.com/wp-content/uploads/2010/01/edit32.png" rel="shadowbox[sbpost-6];player=img;"><img class="size-full wp-image-25 " title="edit3" src="http://blog.pnapieralski.com/wp-content/uploads/2010/01/edit32.png" alt="" width="454" height="91" /></a><p class="wp-caption-text"> </p></div>
<h2>How it works</h2>
<p>When the <em>edit</em> link is clicked, some simple javascript is called.  Here is the PHP code for the <em>edit</em> hyperlink for any joke.</p>
<pre class="brush: php; title: ; notranslate">
echo &quot;&lt;a id=&quot;joke&amp;quot;.$joke_id.&amp;quot;&quot; href=&quot;javascript:edit_joke(&amp;quot;.$joke_id.&amp;quot;);&quot;&gt;;edit&lt;/a&gt;&quot;;
</pre>
<p>We start by assigning each link with a distinct ID (corresponding to the joke&#8217;s ID in the database) and, upon clicking, it calls the javascript function edit_joke.</p>
<h3>The Javascript/jQuery Part</h3>
<p>Inside the edit_joke function, I used jQuery selectors to find the link and text for the joke. I then used the wrapInner() method to place an editable &lt;div&gt; inside our joke text&#8217;s &lt;p&gt; tags (basically, we now have <em>&lt;p id=&#8221;123&#8243;&gt;&lt;div&gt;Your mommy joke hehe haha&lt;/div&gt;&lt;/p&gt;</em> instead of <em>&lt;p id=&#8221;joke123&#8243;&gt;Your mommy joke hehe haha&lt;/p&gt;</em>). After that, change the <em>edit</em> link to <em>submit/cancel</em> using jQuery. Clicking submit will call the javascript function <em>edit_joke_submit(..)</em> and clicking cancel will call the javascript function <em>edit_joke_cancel(..)</em>. I placed those links inside a &lt;span&gt; tag so I could easily reference them later. The magic in this code is the contenteditable=&#8217;true&#8217; part of the div. Here&#8217;s the code:</p>
<pre class="brush: jscript; title: ; notranslate">
function edit_joke(joke_id){
	this_link = $(&quot;a#joke&quot;+joke_id);
	this_joke = $(&quot;p#joke&quot;+joke_id);

	this_joke.wrapInner(&quot;&lt;div contenteditable='true' class='editable_div'&gt;&lt;/div&gt;&quot;);

	this_link.replaceWith(&quot;&lt;span id='submitcancel'&gt;&quot;+
		&quot;&lt;a id='joke&quot;+joke_id+&quot;' href='javascript:edit_joke_submit(&quot;+joke_id+&quot;);'&gt;submit&lt;/a&gt;&quot;+
		&quot;&lt;a id='joke&quot;+joke_id+&quot;' href='javascript:edit_joke_cancel(&quot;+joke_id+&quot;);'&gt;/cancel&lt;/a&gt;&quot; );
}
</pre>
<p>The edit_joke_cancel function is relatively simple. I first found the corresponding <em>submit/cancel</em> link and the joke div using jQuery selectors. After that, I used the html() method to grab the new text that was typed in. I set the text inside the &lt;p&gt; tags equal to the new text so that it would look like <em>&lt;p id=&#8221;123&#8243;&gt;Yo mama joke hehe haha&lt;div&gt;Yo mama joke hehe haha&lt;/div&gt;&lt;/p&gt;</em> instead of <em>&lt;p id=&#8221;joke123&#8243;&gt;Yo mama joke hehe haha&lt;/p&gt;</em>. Then, simply remove the &lt;div&gt; tags (and, consequently, the text inside), and it&#8217;s back to normal.</p>
<pre class="brush: php; title: ; notranslate">
function edit_joke_cancel(joke_id){
	this_link = $(&quot;span#submitcancel&quot;);
	this_joke = $(&quot;p#joke&quot;+joke_id);
	this_joke_div = this_joke.find(&quot;div&quot;);

	joke_text = this_joke_div.html();
	this_joke.html(joke_text);
	this_joke_div.remove();

	this_link.replaceWith(
			&quot;&lt;a id='joke&quot;+joke_id+&quot;' href='javascript:edit_joke(&quot;+joke_id+&quot;);'&gt;edit&lt;/a&gt;&quot;
		);
}
</pre>
<p>To submit the joke changes, we need only send a post request to a server side php script with the joke id and the new joke text. Here&#8217;s the code:</p>
<pre class="brush: jscript; title: ; notranslate">
function edit_joke_submit(joke_id){
	var this_joke = $(&quot;p#joke&quot;+joke_id);
	var joke_text = this_joke.find(&quot;div&quot;).html();

	$.post('/jokes/ajax-edit.php',
			{
				id: joke_id,
				text: joke_text
			},
			function(data){
				alert(data);
			}
		);

	edit_joke_cancel(joke_id); // remove edible div
}
</pre>
<h3>The PHP Part</h3>
<p>All we have to do now is send an update query to the database with the corresponding id and text. The only caveat: editable divs place &lt;br&gt;&#8217;s when you hit enter, so I added the str_replace(..) to turn those into newline characters capable of storage in the database. Check it out:</p>
<pre class="brush: php; title: ; notranslate">
// Include needed files for account verification here.
// Get your database connection and store it in $dbconn HERE. I use mySQLi in my code.
&lt;?php
if( $accounts-&gt;check_admin() )
{
	$id = $dbconn-&gt;escape_string($_POST['id']);    // Prevent sql injection
	$text = $dbconn-&gt;escape_string($_POST['text']);

	$text = str_replace(&quot;&lt;br&gt;&quot;,&quot;\r\n&quot;, $text);    // Swap's out &lt;br&gt;'s

	$query = &quot;UPDATE joke_table SET text='&quot;.$text.&quot;' WHERE id = &quot;.$id;
	$dbconn-&gt;query($query) or die(mysqli_error($dbconn));
	echo &quot;The joke has been edited <img src='http://blog.pnapieralski.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> &quot;;
}
else
{
	echo 'Yea... okay buddy -_-';
}
?&gt;
</pre>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center">
<ul class="socials">
		<li class="shr-twitter">
			<a href="http://www.shareaholic.com/api/share/?title=Inline+AJAX+Editing&amp;link=http://blog.pnapieralski.com/ajax/inline-ajax-editing/&amp;notes=Happypolack.com%20is%20a%20joke%20site%20I%20created%20as%20a%20side%20project%20a%20little%20bit%20ago%20and%20it%27s%20continually%20being%20enhanced.%20The%20latest%20feature%20I%20added%20was%20a%20way%20for%20admins%20to%20edit%20any%20joke%20%28eg%2C%20fix%20typos%29%2C%20quickly%20and%20easily.%0D%0A%0D%0AThis%20weirdo%20typed%20%22Your%20Mommy%22%20instead%20of%20%22Yo%20Mama%22%20on%20one%20of%20the%20jokes.%20How%20distracting%21%20Let%27s%20write%20some%20code%20so%20we%20can%20fix%20this%20on%20the%20fly...&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=%2524%257Btitle%257D%2B-%2B%2524%257Bshort_link%257D&amp;service=7&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-digg">
			<a href="http://www.shareaholic.com/api/share/?title=Inline+AJAX+Editing&amp;link=http://blog.pnapieralski.com/ajax/inline-ajax-editing/&amp;notes=Happypolack.com%20is%20a%20joke%20site%20I%20created%20as%20a%20side%20project%20a%20little%20bit%20ago%20and%20it%27s%20continually%20being%20enhanced.%20The%20latest%20feature%20I%20added%20was%20a%20way%20for%20admins%20to%20edit%20any%20joke%20%28eg%2C%20fix%20typos%29%2C%20quickly%20and%20easily.%0D%0A%0D%0AThis%20weirdo%20typed%20%22Your%20Mommy%22%20instead%20of%20%22Yo%20Mama%22%20on%20one%20of%20the%20jokes.%20How%20distracting%21%20Let%27s%20write%20some%20code%20so%20we%20can%20fix%20this%20on%20the%20fly...&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=3&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-reddit">
			<a href="http://www.shareaholic.com/api/share/?title=Inline+AJAX+Editing&amp;link=http://blog.pnapieralski.com/ajax/inline-ajax-editing/&amp;notes=Happypolack.com%20is%20a%20joke%20site%20I%20created%20as%20a%20side%20project%20a%20little%20bit%20ago%20and%20it%27s%20continually%20being%20enhanced.%20The%20latest%20feature%20I%20added%20was%20a%20way%20for%20admins%20to%20edit%20any%20joke%20%28eg%2C%20fix%20typos%29%2C%20quickly%20and%20easily.%0D%0A%0D%0AThis%20weirdo%20typed%20%22Your%20Mommy%22%20instead%20of%20%22Yo%20Mama%22%20on%20one%20of%20the%20jokes.%20How%20distracting%21%20Let%27s%20write%20some%20code%20so%20we%20can%20fix%20this%20on%20the%20fly...&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=40&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-technorati">
			<a href="http://www.shareaholic.com/api/share/?title=Inline+AJAX+Editing&amp;link=http://blog.pnapieralski.com/ajax/inline-ajax-editing/&amp;notes=Happypolack.com%20is%20a%20joke%20site%20I%20created%20as%20a%20side%20project%20a%20little%20bit%20ago%20and%20it%27s%20continually%20being%20enhanced.%20The%20latest%20feature%20I%20added%20was%20a%20way%20for%20admins%20to%20edit%20any%20joke%20%28eg%2C%20fix%20typos%29%2C%20quickly%20and%20easily.%0D%0A%0D%0AThis%20weirdo%20typed%20%22Your%20Mommy%22%20instead%20of%20%22Yo%20Mama%22%20on%20one%20of%20the%20jokes.%20How%20distracting%21%20Let%27s%20write%20some%20code%20so%20we%20can%20fix%20this%20on%20the%20fly...&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=10&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.shareaholic.com/api/share/?title=Inline+AJAX+Editing&amp;link=http://blog.pnapieralski.com/ajax/inline-ajax-editing/&amp;notes=Happypolack.com%20is%20a%20joke%20site%20I%20created%20as%20a%20side%20project%20a%20little%20bit%20ago%20and%20it%27s%20continually%20being%20enhanced.%20The%20latest%20feature%20I%20added%20was%20a%20way%20for%20admins%20to%20edit%20any%20joke%20%28eg%2C%20fix%20typos%29%2C%20quickly%20and%20easily.%0D%0A%0D%0AThis%20weirdo%20typed%20%22Your%20Mommy%22%20instead%20of%20%22Yo%20Mama%22%20on%20one%20of%20the%20jokes.%20How%20distracting%21%20Let%27s%20write%20some%20code%20so%20we%20can%20fix%20this%20on%20the%20fly...&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=5&amp;tags=&amp;ctype=" rel="nofollow" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.shareaholic.com/api/share/?title=Inline+AJAX+Editing&amp;link=http://blog.pnapieralski.com/ajax/inline-ajax-editing/&amp;notes=Happypolack.com%20is%20a%20joke%20site%20I%20created%20as%20a%20side%20project%20a%20little%20bit%20ago%20and%20it%27s%20continually%20being%20enhanced.%20The%20latest%20feature%20I%20added%20was%20a%20way%20for%20admins%20to%20edit%20any%20joke%20%28eg%2C%20fix%20typos%29%2C%20quickly%20and%20easily.%0D%0A%0D%0AThis%20weirdo%20typed%20%22Your%20Mommy%22%20instead%20of%20%22Yo%20Mama%22%20on%20one%20of%20the%20jokes.%20How%20distracting%21%20Let%27s%20write%20some%20code%20so%20we%20can%20fix%20this%20on%20the%20fly...&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=38&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
</ul><div style="clear: both;"></div><div class="shr-getshr" style="visibility:hidden;font-size:10px !important"><a target="_blank" href="http://www.shareaholic.com/?src=pub">Get Shareaholic</a></div><div style="clear: both;"></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.pnapieralski.com/ajax/inline-ajax-editing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

