<?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>Illuminati Karate Blog &#187; Facebook</title>
	<atom:link href="http://illuminatikarate.com/blog/category/facebook/feed/" rel="self" type="application/rss+xml" />
	<link>http://illuminatikarate.com/blog</link>
	<description></description>
	<lastBuildDate>Wed, 31 Mar 2010 14:12:59 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Submitting a Contact Form via AJAX From Your Facebook Page</title>
		<link>http://illuminatikarate.com/blog/submitting-a-contact-form-via-ajax-from-your-facebook-page/</link>
		<comments>http://illuminatikarate.com/blog/submitting-a-contact-form-via-ajax-from-your-facebook-page/#comments</comments>
		<pubDate>Thu, 03 Dec 2009 03:57:08 +0000</pubDate>
		<dc:creator>George Huger</dc:creator>
				<category><![CDATA[Facebook]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[FBJS]]></category>
		<category><![CDATA[FBML]]></category>

		<guid isPermaLink="false">http://illuminatikarate.com/blog/?p=297</guid>
		<description><![CDATA[We recently created a contact submission form on a client&#8217;s Facebook page, and were not satisfied with the default user experience. For starters, the form opened a new window, and there was no way to redirect the user back to a confirmation message on the Facebook page. 
We decided to submit the form via AJAX [...]]]></description>
			<content:encoded><![CDATA[<p><img src="/blog/wp-content/themes/ikBlog/img/facebook-48x48.png" alt="Facebook" style="float:left;margin:6px 20px 6px 0px;display:inline" />We recently created a contact submission form on a client&#8217;s Facebook page, and were not satisfied with the default user experience. For starters, the form opened a new window, and there was no way to redirect the user back to a confirmation message on the Facebook page. </p>
<p>We decided to submit the form via AJAX instead, and show the confirmation message directly on the Facebook page. This way the user would not have to leave the Facebook page, resulting in a smoother user experience. We couldn&#8217;t find a good explanation of how to do this on Google, so after figuring it out I decided to go ahead and write it up. </p>
<p>The following example will show you how to submit a basic contact form using AJAX from within your Facebook page. All HTML and Javascript that follows should be pasted directly into the &#8220;Edit FBML&#8221; screen on Facebook.</p>
<p><span id="more-297"></span>Here&#8217;s the example form HTML we&#8217;ll use for the rest of this article. It&#8217;s just two text fields (name and email) and a submit button:</p>

<div class="wp_codebox"><table width="100%" ><tr id="p2974"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p297code4"><pre class="html" style="font-family:monospace;">&lt;form action=&quot;http://mywebsite.com/form_submit.php&quot; method=&quot;post&quot;&gt;
     &lt;p&gt;
          &lt;label for=&quot;name&quot;&gt;Name:&lt;/label&gt;
          &lt;input type=&quot;text&quot; name=&quot;name&quot; id=&quot;name&quot; /&gt;
     &lt;/p&gt;
     &lt;p&gt;
          &lt;label for=&quot;email&quot;&gt;Email:&lt;/label&gt;
          &lt;input type=&quot;text&quot; name=&quot;email&quot; id=&quot;email&quot; /&gt;
     &lt;/p&gt;
     &lt;button type=&quot;submit&quot; onclick=&quot;return submitAjaxForm();&quot;&gt;Submit&lt;/button&gt;
     &lt;p id=&quot;ajaxMessage&quot;&gt;&lt;/p&gt;
&lt;/form&gt;</pre></td></tr></table></div>

<p>There&#8217;s three things you should take note of:</p>
<ol>
<li>The form&#8217;s <b>action</b> attribute is set to a page on mywebsite.com, not facebook.com (replace mywebsite.com with your own domain)</li>
<li>The submit button has an onSubmit event that calls a function submitAjaxForm, which we&#8217;ll define shortly</li>
<li>At the end of the form is an empty paragraph tag. We&#8217;ll use this space to display messages to the user.</li>
</ol>
<p>On our server, form_submit.php is a simple PHP script that logs the submissions, and sends back a message for the user letting them know the submission was successful. It might look something like this:</p>

<div class="wp_codebox"><table width="100%" ><tr id="p2975"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p297code5"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
     log_submission<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'email'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <a href="http://www.php.net/echo"><span style="color: #990000;">echo</span></a> <span style="color: #0000ff;">&quot;Thank you for your submission!&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Of course, your script will be more sophisticated than this one, and doesn&#8217;t have to be written in PHP. Its on your server, so you can handle logging the submission however you like. Just make sure the output is valid <a href="http://wiki.developers.facebook.com/index.php/FBML">FBML</a>.</p>
<p>With the form HTML added to your Facebook page and the logging script in place, the only thing left is adding the Javascript to submit the form via AJAX and show the confirmation message.</p>
<p class="note"><b>Note:</b> Facebook does not allow you to use any Javascript function you like, for security purposes. Instead they&#8217;ve created a library of Javascript functions for accessing the DOM, AJAX, and event binding called <a href="http://wiki.developers.facebook.com/index.php/FBJS">FBJS</a>. These functions are used in the example below.</p>
<p>We&#8217;ll now define the submitAjaxForm() function we referenced earlier in our submit button&#8217;s onSubmit event:</p>

<div class="wp_codebox"><table width="100%" ><tr id="p2976"><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
</pre></td><td class="code" id="p297code6"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script<span style="color: #339933;">&gt;&lt;!--</span>
<span style="color: #003366; font-weight: bold;">function</span> submitAjaxForm<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> 
<span style="color: #009900;">&#123;</span> 
	<span style="color: #006600; font-style: italic;">// declare a new FBJS AJAX object</span>
	<span style="color: #003366; font-weight: bold;">var</span> ajax <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Ajax<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
	ajax.<span style="color: #660066;">responseType</span> <span style="color: #339933;">=</span> Ajax.<span style="color: #660066;">FBML</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// define a callback to handle the response from the server</span>
	ajax.<span style="color: #660066;">ondone</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> 
	<span style="color: #009900;">&#123;</span> 
		document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ajaxMessage'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">setInnerFBML</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// let the user know we're sending the data	</span>
	document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ajaxMessage'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">setInnerXHTML</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Submitting your information, please wait...'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// collect field values</span>
	<span style="color: #003366; font-weight: bold;">var</span> queryParams <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> <span style="color: #3366CC;">'name'</span> <span style="color: #339933;">:</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'name'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'email'</span> <span style="color: #339933;">:</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'email'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
	ajax.<span style="color: #660066;">post</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'http://mywebsite.com/form_submit.php'</span><span style="color: #339933;">,</span> queryParams<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: #006600; font-style: italic;">//--&gt;&lt;/script&gt;</span></pre></td></tr></table></div>

<p><em>Note: The opening and closing HTML comments embedded in the Javascript are important, so leave them intact.</em></p>
<p>The example is fairly self explanatory, but here&#8217;s what&#8217;s happening:</p>
<ol>
<li>Declare a new Ajax object, which is one of the FBJS classes. We then set its response type to FBML (Ajax.RAW and Ajax.JSON are also accepted).</li>
<li>Define a callback function to handle the response from the server. In this case, our function simply takes the response from the server and displays it in the #ajaxMessage paragraph we defined earlier.</li>
<li>Update the user to let them know their data is being submitted using FBJS&#8217;s setInnerFBML function.</li>
<li>Collect the form field&#8217;s values into an array named queryParams, using FBJS&#8217;s getElementById and getValue functions. Although getElementById is similar to the standard Javascript function, it accounts for the random slugs Facebook injects into div id&#8217;s for security (View Source on a Facebook page and look at the div ID&#8217;s to see this in action).</li>
<li>Send the AJAX request, using FBJS&#8217; Ajax.post function. The second argument is optional, and should be an array containing the query parameters.</li>
<li>&#8220;return false;&#8221; prevents the form&#8217;s normal submit behavior</li>
</ol>
<p>That&#8217;s all there is to it! If you have any questions or if I should clarify a section, let me know in the comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://illuminatikarate.com/blog/submitting-a-contact-form-via-ajax-from-your-facebook-page/feed/</wfw:commentRss>
		<slash:comments>27</slash:comments>
		</item>
	</channel>
</rss>
