<?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>Point 47 &#187; AJAX</title>
	<atom:link href="http://point47.com/journal/category/ajax/feed/" rel="self" type="application/rss+xml" />
	<link>http://point47.com/journal</link>
	<description>CSS, PHP and ActionScript 3 tutorials</description>
	<lastBuildDate>Tue, 06 Jul 2010 19:11:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>CKEditor 3.0 Send and Receive data with AJAX</title>
		<link>http://point47.com/journal/2009/09/ckeditor-3-0-send-and-receive-data-with-ajax/</link>
		<comments>http://point47.com/journal/2009/09/ckeditor-3-0-send-and-receive-data-with-ajax/#comments</comments>
		<pubDate>Tue, 01 Sep 2009 09:50:11 +0000</pubDate>
		<dc:creator>Sorin</dc:creator>
				<category><![CDATA[AJAX]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://point47.com/journal/?p=49</guid>
		<description><![CDATA[Attention: This is a quick tutorial and no in depth information will be provided. Use Google or whatever you want to learn more about PHP, Javascript and AJAX calls. Also on the CKEditor site you can find more info about the editor itself. The old FCKEditor is now called CKEditor 3.0 and it&#8217;s: CKEditor is]]></description>
			<content:encoded><![CDATA[<p><em>Attention: This is a quick tutorial and no in depth information will be provided. Use Google or whatever you want to learn more about PHP, Javascript and AJAX calls. Also on the CKEditor site you can find more info about the editor itself. </em></p>
<p>The old FCKEditor is now called CKEditor 3.0 and it&#8217;s:</p>
<blockquote><p>CKEditor is a text editor to be used inside web pages. It&#8217;s a WYSIWYG editor, which means that the text being edited on it looks as similar as possible to the results users have when publishing it. It brings to the web common editing features found on desktop editing applications like Microsoft Word and OpenOffice.</p></blockquote>
<p>You can download it from here: <a href="http://ckeditor.com">http://ckeditor.com</a>.</p>
<p>OK.. so I wanted to edit some HTML template files from an Admin area module and the new CKEditor looks pretty nice so I gave it a try.<br />
First I install the CKEditor and this is pretty easy to do, just download the editor and copy the files to the server.</p>
<p>Create a link to the editor:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:610px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&lt;script type=&quot;text/javascript&quot; src=&quot;/ckeditor/ckeditor.js&quot;&gt;&lt;/script&gt;</div></td></tr></tbody></table></div>
<p>and then creat a text field area:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:610px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&lt;textarea name=&quot;template_body&quot; id=&quot;template_body&quot; &gt;page content here&lt;/textarea&gt;</div></td></tr></tbody></table></div>
<p>and add the javascript code to load the editor:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:610px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&lt;script type=&quot;text/javascript&quot;&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CKEDITOR.replace( 'template_body' );<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;/script&gt;</div></td></tr></tbody></table></div>
<p>I added also a div to hold the received messages from the AJAX calls:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:610px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&lt;div id=&quot;saveButton&quot; style=&quot;font-weight:bold&quot;&gt;&lt;/div&gt;</div></td></tr></tbody></table></div>
<p>Then I generated a tree/file view of all the template files(for sake of simplicity I will not show you how to do that).<br />
The links are generated like this:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:610px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&lt;a href=&quot;&quot; onclick=&quot;getFile('test.html'); return false;&quot; id=&quot;file&quot; &gt;test.html&lt;/a&gt;</div></td></tr></tbody></table></div>
<p>The return false is so than I won&#8217;t need to use the stupid # on the href=&#8221;" so that the page won&#8217;t refresh.</p>
<p>And the two Javascript functions that make the Ajax calls are:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:610px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&lt;script type=&quot;text/javascript&quot;&gt;<br />
function getFile(file)<br />
{ &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; var httpRequest = (!window.XMLHttpRequest)? new ActiveXObject(&quot;Microsoft.XMLHTTP&quot;) : new XMLHttpRequest();<br />
&nbsp; &nbsp; httpRequest.onreadystatechange = function()<br />
&nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; if(httpRequest.readyState == 4)<br />
&nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // send info to editor<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CKEDITOR.instances.template_body.setData(httpRequest.responseText);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById('saveButton').innerHTML = '&lt;a href=&quot;&quot; onclick=&quot;saveFile(\'' + file + '\'); return false;&quot; &gt;SAVE TEMPLATE&lt;/a&gt;';<br />
&nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; }<br />
&nbsp; &nbsp; httpRequest.open(&quot;GET&quot;,&quot;/ajax/get_file.php?file=&quot;+file,true);<br />
&nbsp; &nbsp; httpRequest.send(null); &nbsp; &nbsp; &nbsp;<br />
}<br />
<br />
function saveFile(filename)<br />
{ &nbsp; &nbsp;<br />
&nbsp; &nbsp; // get info from editor<br />
&nbsp; &nbsp; var editordata = CKEDITOR.instances.template_body.getData();<br />
<br />
&nbsp; &nbsp; var httpRequest = (!window.XMLHttpRequest)? new ActiveXObject(&quot;Microsoft.XMLHTTP&quot;) : new XMLHttpRequest();<br />
&nbsp; &nbsp; httpRequest.onreadystatechange = function()<br />
&nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; if(httpRequest.readyState == 4)<br />
&nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(httpRequest.responseText == &quot;success&quot;)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById('saveButton').innerHTML = '&lt;span style=&quot;color:red&quot;&gt;Template saved&lt;/span&gt;';<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById('saveButton').innerHTML = httpRequest.responseText;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; }<br />
&nbsp; &nbsp; var passingData = &quot;file=&quot;+filename+&quot;&amp;template_body=&quot;+ escape(editordata)<br />
&nbsp; &nbsp; httpRequest.open(&quot;POST&quot;, &quot;/ajax/save_file.php?&quot;+passingData, true);<br />
&nbsp; &nbsp; httpRequest.setRequestHeader(&quot;Content-type&quot;, &quot;application/x-www-form-urlencoded&quot;); &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; httpRequest.send(passingData);<br />
}<br />
&lt;/script&gt;</div></td></tr></tbody></table></div>
<p>The first function <strong>getFile()</strong>, gets the name of the file to load and send the response text from the PHP script to the editor.<br />
The PHP script gets the name of the file and with file_get_contents function I retrieve that template&#8217;s contents.</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:610px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">echo file_get_contents('/templates/'.$_GET['file']);</div></td></tr></tbody></table></div>
<p>The editor shows the received data with the command:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:610px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">CKEDITOR.instances.template_body.setData(httpRequest.responseText);</div></td></tr></tbody></table></div>
<p>&#8220;template_body&#8221; is the name of the text field that is replaced by the CKEditor.<br />
In this function I also add a new link to the page with innerHTML that makes the reference to the second function: <strong>saveFile()</strong>.</p>
<p>The second function <strong>saveFile()</strong> get the current name of the loaded template (test.html for example) and saves the data from the editor to<br />
a variable &#8220;editordata&#8221; with the call:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:610px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">CKEDITOR.instances.template_body.getData();</div></td></tr></tbody></table></div>
<p>The editor data is sent to the PHP script, escaped and urlencoded with POST.</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:610px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">// write content to file <br />
$filename = '/templates/'.$_POST['file'];<br />
$template_body = $_POST['template_body'];<br />
<br />
if (is_writable($filename)) // check file first<br />
{<br />
&nbsp; &nbsp; if (!$handle = fopen($filename, 'w')) &nbsp;// open file<br />
&nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;echo 'fail open';<br />
&nbsp; &nbsp; }<br />
<br />
&nbsp; &nbsp; if (fwrite($handle, $template_body) === FALSE) // write file<br />
&nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; echo 'fail write';<br />
&nbsp; &nbsp; }<br />
<br />
&nbsp; &nbsp; fclose($handle); // and close the file<br />
&nbsp; &nbsp; echo 'success';<br />
} <br />
else <br />
{<br />
&nbsp; &nbsp; echo 'fail not writable';<br />
}</div></td></tr></tbody></table></div>
<p>The PHP code is straight forward: open file, write contents to file, close file.<br />
If success is returned then with innerHTML we write that the template was saved if not the specific fail message will be shown.<br />
(usually those errors appear if the file is missing or the file path is wrong or the file permissions are wrong.)</p>
<p>So here you have it an browser based template editor.<br />
Oh and if your template has</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:610px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&lt;html&gt;, &lt;body&gt;</div></td></tr></tbody></table></div>
<p>tags they will be deleted by the editor. Don&#8217;t know how to override that shit functionality.</p>
<p>Over and out.</p>
<p>Hey did you know about this?<br />
<a href="http://themeforest.net?ref=forapathy" title="wordpress themes, site templates, psd" target="_blank"><img src="http://envato.s3.amazonaws.com/referrer_adverts/tf_728x90_v5.gif" alt="wordpress themes, site templates" /></a></p>
<img src="http://point47.com/journal/?ak_action=api_record_view&id=49&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://point47.com/journal/2009/09/ckeditor-3-0-send-and-receive-data-with-ajax/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
	</channel>
</rss>
