<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>King Tutz Web - Learn PHP, Javascript, Ruby &amp; More!</title>
	
	<link>http://web.kingtutz.com</link>
	<description>Kingtutz.com Tutorials, Advice, Photoshop Learning, Entrepreneurial, and More!</description>
	<lastBuildDate>Fri, 26 Aug 2011 14:31:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/KingTutzWeb-LearnPhpJavascriptRubyMore" /><feedburner:info uri="kingtutzweb-learnphpjavascriptrubymore" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Quick Tip: Pure CSS Triangles</title>
		<link>http://feedproxy.google.com/~r/KingTutzWeb-LearnPhpJavascriptRubyMore/~3/VUNSPxmLMeA/</link>
		<comments>http://web.kingtutz.com/2011/08/26/quick-tip-pure-css-triangles/#comments</comments>
		<pubDate>Fri, 26 Aug 2011 14:31:13 +0000</pubDate>
		<dc:creator>Justin Podzimek</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[Quick Tips]]></category>

		<guid isPermaLink="false">http://web.kingtutz.com/?p=260</guid>
		<description><![CDATA[Pure CSS triangles are one of those clientside development hidden gems that few know about. Let's take a look at what it takes to create a triangle that's compatible across all browsers (including IE7+) using nothing more than a few lines of CSS.]]></description>
			<content:encoded><![CDATA[<p style="text-align: center">
<a href="http://kingtutz.com/demopages/web/css-triangles/index.html" target="_blank"><img class="dtse-img dtse-post-260" class="dtse-img dtse-post-260" src="http://web.kingtutz.com/files/2011/08/demo.png" alt="View the demo" border="0" /></a>
</p>
<h3>Understanding the Concept</h3>
<p>
Everyone knows how to add a border to an element, nothing really shocking there. But something that many people might not know, is that browsers draw borders at an angle from the center. Using this technique to our advantage, we can set 3 border colors of an element to transparent, leaving our remaining side to render out a triangle.
</p>
<h3>Creating the Base Class</h3>
<p>
To make our triangles easy to modify, we&#8217;ll start with the base class of <code>.triangle</code> that will contain the modifiable parameters to make our triangle look the way we want. This is where you can change the values to coordinate with your own application.
</p>
<pre class="brush: css; title: ; notranslate">
.triangle {
    border-width: 50px;
    border-style: solid;
    border-color: #FFF;
    width: 0;
    height: 0;
    *zoom: 1;
}
</pre>
<p>
Nothing too exciting yet, we&#8217;ve create a basic block with a 50px solid white border. Notice that we set the width and height to 0, this is to allow the border to create our triangle without any extra spacing in the middle of our element. And finally, we add the zoom parameter to target IE7 and below and let those browsers know that this element has a layout.
</p>
<h3>Building the Triangles</h3>
<p>
Now that we have a basic 4 sided white square, we can add some subclasses to override 3 of the sides and set their border color to transparent, leaving just the remaining side and our triangle.
</p>
<pre class="brush: css; title: ; notranslate">
.triangle.up {
    border-top: 0 solid transparent;
    border-right-color: transparent;
    border-left-color: transparent;
}
.triangle.right {
    border-right: 0 solid transparent;
    border-top-color: transparent;
    border-bottom-color: transparent;
}
.triangle.down {
    border-bottom: 0 solid transparent;
    border-right-color: transparent;
    border-left-color: transparent;
}
.triangle.left {
    border-left: 0 solid transparent;
    border-top-color: transparent;
    border-bottom-color: transparent;
}
</pre>
<p>
Notice that on the side our triangle is pointing, in addition to setting the border color to transparent, we also set the width to 0. This is to ensure that we don&#8217;t have any extra spacing in the empty space of our element. We want the element to only be the size of what we can see, nothing more.
</p>
<h3>Adding the Markup</h3>
<p>
Now that we have the styles for our triangles, the last thing we need to do is include the markup to display them.
</p>
<pre class="brush: xml; title: ; notranslate">
&lt;div class=&quot;triangle up&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;triangle right&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;triangle down&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;triangle left&quot;&gt;&lt;/div&gt;
</pre>
<h3>Conclusion</h3>
<p>
Hopefully this quick tip gave you everything you need to start creating your own CSS triangles. If you&#8217;re having any troubles getting your markup and styles to play nicely, drop me a line in the comments and we&#8217;ll work on getting everything sorted out. Thanks for reading!
</p>
<p style="text-align: center">
<a href="http://kingtutz.com/demopages/web/css-triangles/index.html" target="_blank"><img class="dtse-img dtse-post-260" class="dtse-img dtse-post-260" src="http://web.kingtutz.com/files/2011/08/demo.png" alt="View the demo" border="0" /></a></p>



		<!-- Added by WP-DragToShare-eXtended Plugin -->
		<script type="text/javascript">
			dtsv.dtse_post_260_permalink = 'http://web.kingtutz.com/2011/08/26/quick-tip-pure-css-triangles/';
			dtsv.dtse_post_260_title = 'Quick Tip: Pure CSS Triangles';
		</script>
		<!-- End of WP-DragToShare-eXtended Plugin --><img src="http://feeds.feedburner.com/~r/KingTutzWeb-LearnPhpJavascriptRubyMore/~4/VUNSPxmLMeA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://web.kingtutz.com/2011/08/26/quick-tip-pure-css-triangles/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://web.kingtutz.com/2011/08/26/quick-tip-pure-css-triangles/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=quick-tip-pure-css-triangles</feedburner:origLink></item>
		<item>
		<title>Create an Ajax Driven Comment Form</title>
		<link>http://feedproxy.google.com/~r/KingTutzWeb-LearnPhpJavascriptRubyMore/~3/s09JtvUMzZo/</link>
		<comments>http://web.kingtutz.com/2011/08/18/create-an-ajax-driven-comment-form/#comments</comments>
		<pubDate>Thu, 18 Aug 2011 14:22:23 +0000</pubDate>
		<dc:creator>Justin Podzimek</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://web.kingtutz.com/?p=218</guid>
		<description><![CDATA[Giving a form the ability to send data asynchronously can give your site an extra degree of professionalism. Plus, it's not too difficult to pull of with just a few extra lines of code. In today's post, we'll create an ajax driven comment form that you can add to your own personal blog. Let's get started!]]></description>
			<content:encoded><![CDATA[<a href="http://web.kingtutz.com/wp-content/plugins/download-monitor/download.php?id=2" title="Downloaded 75 times"><img src="http://web.kingtutz.com/files/2011/08/download.png" alt="Ajax Comment Form" /></a><a href="http://kingtutz.com/demopages/web/ajax-form/index.html" target="_blank"><img src="http://web.kingtutz.com/files/2011/08/demo.png" alt="View the demo" border="0" /></a>
</p>
<p>
<h3>What We&#8217;ll Be Creating</h3>
<p>
<a href="http://web.kingtutz.com/files/2011/08/ajax-form-preview.png"><br />
<img class="aligncenter size-full wp-image-1690 tutorial_image dtse-img dtse-post-1729 ui-draggable dtse-img dtse-post-218" title="Final" src="http://web.kingtutz.com/files/2011/08/ajax-form-preview.png" alt="Final" width="530" height="234" original="http://web.kingtutz.com/files/2011/08/ajax-form-preview.png"><br />
</a>
</p>
<h3><span>Step One:</span> Building the Base</h3>
<p>
Before we dive into the code, let&#8217;s take a minute and set up our folder structure. In the root of your main project folder, let&#8217;s add a few folders: <code>css</code>, <code>images</code>, <code>javascript</code>, and <code>php</code>.
</p>
<p>
Looking ahead, we know that our form will be running <a href="http://jquery.com/" target="_blank">jQuery</a>. At the time of writing this, the current release is at v1.6.2, so make sure you&#8217;re running at least that version. Download the minified version and stick it in your <code>javascript</code> folder (rename it to jquery.min.js if it&#8217;s not already named that).
</p>
<p>
Our form will also be using a few small icons. Go ahead the download these images into your <code>images</code> folder.
</p>
<ul>
<li><a href="http://web.kingtutz.com/files/2011/08/loader.gif">loader.gif</a></li>
<li><a href="http://web.kingtutz.com/files/2011/08/error.png">error.png</a></li>
<li><a href="http://web.kingtutz.com/files/2011/08/success.png">success.png</a></li>
</ul>
<p>
Special thanks to <a href="http://www.ajaxload.info/" target="_blank">ajaxload.info</a> for the animated loader and <a href="http://commons.wikimedia.org/wiki/Crystal_Clear" target="_blank">Crystal Clear</a> for our awesome error and success images. Now that we&#8217;ve got the foundation for our application, let&#8217;s start building the good stuff: the code!
</p>
<h3><span>Step Two:</span> Creating the Markup</h3>
<p>
We&#8217;ll get started in our code by creating the markup. In your root project folder, create a new file and name it <code>index.html</code>. Add in the following markup as a base, and we&#8217;ll build on it from there.
</p>
<pre class="brush: xml; title: ; notranslate">
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;
&lt;html&gt;
    &lt;head&gt;
    	&lt;title&gt;Ajax Comment Submission Form&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;

    &lt;/body&gt;
&lt;/html&gt;
</pre>
<p>
Now that we have our basic HTML template, let&#8217;s add the form markup. To prevent page bloat, we want to use as little tags as possible while still allowing ourselves enough room to style it up the way we want. Copy the following into your <code>index.html</code> page.
</p>
<pre class="brush: xml; title: ; notranslate">
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;
&lt;html&gt;
    &lt;head&gt;
    	&lt;title&gt;Ajax Comment Submission Form&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;form id=&quot;comment-form&quot; action=&quot;php/comment.php&quot; method=&quot;POST&quot;&gt;
            &lt;p&gt;
                &lt;label for=&quot;email&quot;&gt;
                    &lt;span&gt;*&lt;/span&gt; Email:
                &lt;/label&gt;
                &lt;input type=&quot;text&quot; id=&quot;email&quot; name=&quot;email&quot; /&gt;
            &lt;/p&gt;
            &lt;p&gt;
                &lt;label for=&quot;comment&quot;&gt;
                    &lt;span&gt;*&lt;/span&gt; Comment:
                &lt;/label&gt;
                &lt;textarea id=&quot;comment&quot; name=&quot;comment&quot;&gt;&lt;/textarea&gt;
            &lt;/p&gt;
            &lt;p class=&quot;comment-right&quot;&gt;
                &lt;input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Submit&quot; /&gt;
            &lt;/p&gt;
            &lt;p class=&quot;comment-center&quot; id=&quot;loader&quot;&gt;
                &lt;img src=&quot;images/loader.gif&quot; alt=&quot;Sending...&quot; border=&quot;0&quot; /&gt;
            &lt;/p&gt;
            &lt;p class=&quot;comment-center&quot; id=&quot;success&quot;&gt;
                &lt;img src=&quot;images/success.png&quot; alt=&quot;Success!&quot; border=&quot;0&quot; /&gt; Your comment has been submitted successfully!
            &lt;/p&gt;
            &lt;p class=&quot;comment-center&quot; id=&quot;error&quot;&gt;
                &lt;img src=&quot;images/error.png&quot; alt=&quot;Error!&quot; border=&quot;0&quot; /&gt; Sorry, your comment can't be added. Please try again later!
            &lt;/p&gt;
        &lt;/form&gt;
        &lt;br class=&quot;clear&quot; /&gt;
    &lt;/body&gt;
&lt;/html&gt;
</pre>
<p>
Much of what we just added to the HTML might not have a clear purpose just yet, but stick around, everything will soon fall into place. Now that we have our basic markup, we&#8217;ll need to add references to our stylesheet and JavaScript that we&#8217;ll be creating shortly. So in the header section, let&#8217;s add our includes.
</p>
<pre class="brush: xml; title: ; notranslate">
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;
&lt;html&gt;
    &lt;head&gt;
    	&lt;title&gt;Ajax Comment Submission Form&lt;/title&gt;
    	&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;css/comment.css&quot; /&gt;
    	&lt;script type=&quot;text/javascript&quot; src=&quot;javascript/jquery.min.js&quot;&gt;&lt;/script&gt;
    	&lt;script type=&quot;text/javascript&quot; src=&quot;javascript/comment.js&quot;&gt;&lt;/script&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;form id=&quot;comment-form&quot; action=&quot;php/comment.php&quot; method=&quot;POST&quot;&gt;
            &lt;p&gt;
                &lt;label for=&quot;email&quot;&gt;
                    &lt;span&gt;*&lt;/span&gt; Email:
                &lt;/label&gt;
                &lt;input type=&quot;text&quot; id=&quot;email&quot; name=&quot;email&quot; /&gt;
            &lt;/p&gt;
            &lt;p&gt;
                &lt;label for=&quot;comment&quot;&gt;
                    &lt;span&gt;*&lt;/span&gt; Comment:
                &lt;/label&gt;
                &lt;textarea id=&quot;comment&quot; name=&quot;comment&quot;&gt;&lt;/textarea&gt;
            &lt;/p&gt;
            &lt;p class=&quot;comment-right&quot;&gt;
                &lt;input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Submit&quot; /&gt;
            &lt;/p&gt;
            &lt;p class=&quot;comment-center&quot; id=&quot;loader&quot;&gt;
                &lt;img src=&quot;images/loader.gif&quot; alt=&quot;Sending...&quot; border=&quot;0&quot; /&gt;
            &lt;/p&gt;
            &lt;p class=&quot;comment-center&quot; id=&quot;success&quot;&gt;
                &lt;img src=&quot;images/success.png&quot; alt=&quot;Success!&quot; border=&quot;0&quot; /&gt; Your comment has been submitted successfully!
            &lt;/p&gt;
            &lt;p class=&quot;comment-center&quot; id=&quot;error&quot;&gt;
                &lt;img src=&quot;images/error.png&quot; alt=&quot;Error!&quot; border=&quot;0&quot; /&gt; Sorry, your comment can't be added. Please try again later!
            &lt;/p&gt;
        &lt;/form&gt;
        &lt;br class=&quot;clear&quot; /&gt;
    &lt;/body&gt;
&lt;/html&gt;
</pre>
<p>
Once we create the JavaScript for our form, we&#8217;re going to need an instantiation for it. So the last thing we&#8217;ll want to do in our <code>index.html</code> file is add the instantiation for the plugin that we&#8217;ll be creating in a little bit. Just before the closing <code>&lt;body /&gt;</code> tag, add the final code for the plugin.
</p>
<pre class="brush: xml; title: ; notranslate">
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;
&lt;html&gt;
    &lt;head&gt;
    	&lt;title&gt;Ajax Comment Submission Form&lt;/title&gt;
    	&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;css/comment.css&quot; /&gt;
    	&lt;script type=&quot;text/javascript&quot; src=&quot;javascript/jquery.min.js&quot;&gt;&lt;/script&gt;
    	&lt;script type=&quot;text/javascript&quot; src=&quot;javascript/comment.js&quot;&gt;&lt;/script&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;form id=&quot;comment-form&quot; action=&quot;php/comment.php&quot; method=&quot;POST&quot;&gt;
            &lt;p&gt;
                &lt;label for=&quot;email&quot;&gt;
                    &lt;span&gt;*&lt;/span&gt; Email:
                &lt;/label&gt;
                &lt;input type=&quot;text&quot; id=&quot;email&quot; name=&quot;email&quot; /&gt;
            &lt;/p&gt;
            &lt;p&gt;
                &lt;label for=&quot;comment&quot;&gt;
                    &lt;span&gt;*&lt;/span&gt; Comment:
                &lt;/label&gt;
                &lt;textarea id=&quot;comment&quot; name=&quot;comment&quot;&gt;&lt;/textarea&gt;
            &lt;/p&gt;
            &lt;p class=&quot;comment-right&quot;&gt;
                &lt;input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Submit&quot; /&gt;
            &lt;/p&gt;
            &lt;p class=&quot;comment-center&quot; id=&quot;loader&quot;&gt;
                &lt;img src=&quot;images/loader.gif&quot; alt=&quot;Sending...&quot; border=&quot;0&quot; /&gt;
            &lt;/p&gt;
            &lt;p class=&quot;comment-center&quot; id=&quot;success&quot;&gt;
                &lt;img src=&quot;images/success.png&quot; alt=&quot;Success!&quot; border=&quot;0&quot; /&gt; Your comment has been submitted successfully!
            &lt;/p&gt;
            &lt;p class=&quot;comment-center&quot; id=&quot;error&quot;&gt;
                &lt;img src=&quot;images/error.png&quot; alt=&quot;Error!&quot; border=&quot;0&quot; /&gt; Sorry, your comment can't be added. Please try again later!
            &lt;/p&gt;
        &lt;/form&gt;
        &lt;br class=&quot;clear&quot; /&gt;
        &lt;script type=&quot;text/javascript&quot;&gt;
        (function ($) {
            $('form#comment-form').commentForm();
        }(jQuery));
        &lt;/script&gt;
    &lt;/body&gt;
&lt;/html&gt;
</pre>
<h3><span>Step Three:</span> Adding the Styles</h3>
<p>
Now that we have our markup in place, it&#8217;s time to style this form up. In your <code>css</code> folder, create a new file called <code>comment.css</code>. We&#8217;ll start with a reset to make the styles as uniform as possible across all browsers.
</p>
<pre class="brush: css; title: ; notranslate">
/* -- reset */
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
	margin: 0;
	padding: 0;
	border: 0;
	font-size: 100%;
	font: inherit;
	vertical-align: baseline;
}
article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
	display: block;
}
body {
	line-height: 1;
}
ol, ul {
	list-style: none;
}
blockquote, q {
	quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
	content: '';
	content: none;
}
table {
	border-collapse: collapse;
	border-spacing: 0;
}
br.clear {
    clear: both;
}
</pre>
<p>
Now that we have the reset in place, let&#8217;s start styling out the form itself. You&#8217;ll notice there are a couple CSS hacks (the asterisk targets IE7 and below), but they are necessary to make sure the form looks the same across all browsers (IE6+). Go ahead and add the following into your <code>comment.css</code> right below your reset code.
</p>
<pre class="brush: css; title: ; notranslate">
/* -- comment form */
body {
    background: #597d92;
    color: #333333;
    font: normal 16px/16px arial, sans-serif;
}
#comment-form {
    background: #f2f2f1;
    -moz-border-radius: 5px;
    border-radius: 5px;
    display: inline-block;
    padding: 10px 38px;
    *display: block;
    *float: left;
}
#comment-form p {
    padding: 10px 0;
}
#comment-form p.comment-right {
    text-align: right;
}
#comment-form p.comment-center {
    text-align: center;
}
#comment-form #loader,
#comment-form #success,
#comment-form #error {
    display: none;
}
#comment-form label {
    display: block;
    padding-bottom: 5px;
}
#comment-form label span {
    color: #b90202;
}
#comment-form input[type=text],
#comment-form textarea {
    width: 320px;
    background-color: #FFF;
    border: 1px solid #d7d7d5;
    padding: 5px 3px;
    outline: 0;
}
#comment-form input[type=text].error,
#comment-form textarea.error {
    border: 1px solid #b90202;
}
#comment-form textarea {
    height: 50px;
    width: 500px;
    min-height: 50px;
    resize: vertical;
    font-family: arial, sans-serif;
    overflow: auto;
}
#comment-form input[type=submit] {
    font: normal 16px/16px arial, sans-serif;
    border-width: 0;
    background-color: #b90202;
    color: #f2f2f1;
    font-size: 16px;
    padding: 5px 8px;
    cursor: pointer;
}
</pre>
<p>
If you test your form now, you&#8217;ll notice that it looks just about correct, but not exactly the same as the demo. To fix the alignment issue, we&#8217;ll add in a little extra code which should only be used for the purpose of this demo. When you add the form into your own blog, you&#8217;ll want to remove the &#8220;demo only&#8221; section.
</p>
<pre class="brush: css; title: ; notranslate">
/* -- demo only */
body {
    text-align: center;
}
#comment-form {
    text-align: left;
    margin: 50px auto 0 auto;
}
</pre>
<p>
So your final code in your <code>comment.css</code> should now look like the following. Keep in mind that when you integrate this code into your own blog or website, you may need to tweak some styles to make it play nicely with your other markup.
</p>
<pre class="brush: css; title: ; notranslate">
/* -- reset */
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
	margin: 0;
	padding: 0;
	border: 0;
	font-size: 100%;
	font: inherit;
	vertical-align: baseline;
}
article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
	display: block;
}
body {
	line-height: 1;
}
ol, ul {
	list-style: none;
}
blockquote, q {
	quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
	content: '';
	content: none;
}
table {
	border-collapse: collapse;
	border-spacing: 0;
}
br.clear {
    clear: both;
}
/* -- comment form */
body {
    background: #597d92;
    color: #333333;
    font: normal 16px/16px arial, sans-serif;
}
#comment-form {
    background: #f2f2f1;
    -moz-border-radius: 5px;
    border-radius: 5px;
    display: inline-block;
    padding: 10px 38px;
    *display: block;
    *float: left;
}
#comment-form p {
    padding: 10px 0;
}
#comment-form p.comment-right {
    text-align: right;
}
#comment-form p.comment-center {
    text-align: center;
}
#comment-form #loader,
#comment-form #success,
#comment-form #error {
    display: none;
}
#comment-form label {
    display: block;
    padding-bottom: 5px;
}
#comment-form label span {
    color: #b90202;
}
#comment-form input[type=text],
#comment-form textarea {
    width: 320px;
    background-color: #FFF;
    border: 1px solid #d7d7d5;
    padding: 5px 3px;
    outline: 0;
}
#comment-form input[type=text].error,
#comment-form textarea.error {
    border: 1px solid #b90202;
}
#comment-form textarea {
    height: 50px;
    width: 500px;
    min-height: 50px;
    resize: vertical;
    font-family: arial, sans-serif;
    overflow: auto;
}
#comment-form input[type=submit] {
    font: normal 16px/16px arial, sans-serif;
    border-width: 0;
    background-color: #b90202;
    color: #f2f2f1;
    font-size: 16px;
    padding: 5px 8px;
    cursor: pointer;
}
/* -- demo only */
body {
    text-align: center;
}
#comment-form {
    text-align: left;
    margin: 50px auto 0 auto;
}
</pre>
<h3><span>Step Four:</span> Creating the Plugin</h3>
<p>
Now that we have our form all styled out, it&#8217;s time to add some functionality to it. You should have downloaded jQuery (at least version 1.6.2) and have it named <code>jquery.min.js</code> in your <code>javascript</code> folder, so if not, go ahead and do it now.
</p>
<p>
Next, create a new file named <code>comment.js</code> and place it in your <code>javascript</code> folder. This will contain our jQuery plugin for our form. We&#8217;ll start off by creating the base for our plugin. If you&#8217;re confused about jQuery plugins or want to learn more about them, take a look at our tutorial &#8220;<a href="http://web.kingtutz.com/2011/07/23/the-basics-building-a-jquery-plugin/">The Basics: Building a jQuery Plugin</a>&#8220;.
</p>
<pre class="brush: jscript; title: ; notranslate">
(function ($) {
    $.fn.commentForm = function (opts) {
        var options = $.extend({
            speed: 200
        }, opts);
        return this.each(function () {

            var base = this,
                $base = $(this);

            this.init = function () {

            };
            this.init();
        });
    };
}(jQuery));
</pre>
<p>
Notice we named out plugin &#8220;commentForm&#8221;, which, if you recall, is what we called at the bottom of our <code>index.html</code> file. We added in a default variable <code>speed</code> which will contain the speed of our animations during the data-sending period. We created 2 private variables: <code>base</code>, which holds a reference to the element passed in during our instantiation, and <code>$base</code>, which holds in a jQuery object of the same element.
</p>
<p>
The following code will complete the plugin. I&#8217;ve commented out each function, so it shouldn&#8217;t be too difficult to see the process of the plugin on form submission. In a nutshell, we&#8217;ll be doing the following:
</p>
<ul>
<li>Catch the form submission to prevent default behavior</li>
<li>Process the data to make sure no values are empty</li>
<li>Display any errors and stop the submission, or build the query to send to the server</li>
<li>Send the data to the server</li>
<li>Show a success or error message depending on the message from the server</li>
</ul>
<pre class="brush: jscript; title: ; notranslate">
(function ($) {
    $.fn.commentForm = function (opts) {
        var options = $.extend({
            speed: 200
        }, opts);
        return this.each(function () {

            var base = this,
                $base = $(this);

            /**
             * Initialize the plugin
             **/
            this.init = function () {
                $base.bind('submit.commentForm', base.catchSubmit);
            };

            /**
             * Catch the form submission to prevent the default form action
             **/
            this.catchSubmit = function () {
                base.hideAll();
                if (!base.checkErrors()) {
                    base.showLoader();
                    base.submitData();
                }
                return false;
            };

            /**
             * Submit the data to the server
             **/
            this.submitData = function () {
                var query = base.buildQuery();
                $.ajax({
                    type: $base.attr('method'),
                    url: $base.attr('action'),
                    data: query,
                    success: function (d) {
                        base.hideLoader();
                        if (d === 'true') {
                            base.successPost();
                        } else {
                            base.failPost();
                        }
                    },
                    error: function () {
                        base.hideLoader();
                        base.failPost();
                    }
                });
            };

            /**
             * Show the loader icon
             **/
            this.showLoader = function () {
                $base.find('#loader').slideDown(options.speed);
            };

            /**
             * Hide the loader icon
             **/
            this.hideLoader = function () {
                $base.find('#loader').slideUp(options.speed);
            };

            /**
             * Hide the error and success messages
             **/
            this.hideAll = function () {
                $base.find('#success, #error').slideUp(options.speed);
            };

            /**
             * Show the success message after the data has posted
             **/
            this.successPost = function () {
                setTimeout(function () {
                    $base.find('#success').slideDown(options.speed);
                    base.clearForm();
                }, options.speed * 2);
            };

            /**
             * Show the error message if something went wrong when submitting the data
             **/
            this.failPost = function () {
                setTimeout(function () {
                    $base.find('#error').slideDown(options.speed);
                }, options.speed * 2);
            };

            /**
             * Clear all data in the form
             **/
            this.clearForm = function () {
                $base.find('input[type=text], textarea').val('');
            };

            /**
             * Create the query of data to send to the server
             * @return (String) the query
             **/
            this.buildQuery = function () {
                var q = '';
                $base.find('input, textarea').each(function () {
                    q += escape($(this).attr('name')) + '=' + escape($(this).val()) + '&amp;';
                });
                return q;
            };

            /**
             * Check for any empty values in the form.
             * Any other validation should be done in this function.
             * @return (Boolean) true if errors, false if none
             **/
            this.checkErrors = function () {
                var errors = false,
                    $elem;
                $base.find('input[type!=submit], textarea').each(function () {
                    $elem = $(this);
                    if ($.trim($elem.val()) === '') {
                        $elem.addClass('error');
                        errors = true;
                    } else {
                        $elem.removeClass('error');
                    }
                });
                return errors;
            };
            this.init();
        });
    };
}(jQuery));
</pre>
<h3><span>Step Five:</span> Setting Up the Database</h3>
<p>
Before we jump into our server-side code, we need to set up the database and table that will hold our data. Hop into your database admin control, create a new database, and give it a name. Then, create a new table in that database and give it a name with 3 fields:
</p>
<ol>
<li>&#8216;id&#8217; INT NOT NULL AUTO_INCREMENT PRIMARY KEY</li>
<li>&#8216;email&#8217; VARCHAR( 255 ) NOT NULL</li>
<li>&#8216;comment&#8217; TEXT NOT NULL</li>
</ol>
<h3><span>Step Six:</span> Catching the Data</h3>
<p>
Now that we have our plugin created and our form is styled up, it&#8217;s time to jump to the server code to catch the data and insert it into the database. In our <code>php</code> folder, let&#8217;s create 3 files: <code>conf.php</code>, <code>comment.php</code>, and <code>comment.class.php</code>. Open up <code>conf.php</code> and add the following:
</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
define('HOST', '');
define('USERNAME', '');
define('PASSWORD', '');
define('DATABASE', '');
define('TABLE', '');
</pre>
<p>
This file will contain the values to connect to our database. Add in your personal information for the host, username, password, database, and table. Use the same names of the database and table that you used when creating your database back in step five.
</p>
<p>
Create another file in your <code>php</code> directory named <code>comment.class.php</code> and add in the following code:
</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
class Comment {

}
</pre>
<p>
This file will contain all the methods for our application server-side. Let&#8217;s add in a function to connect to our server and select the database. Notice we&#8217;re using the constants we declared in <code>conf.php</code>.
</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
class Comment {
    private static function connect() {
        mysql_connect(HOST, USERNAME, PASSWORD);
        mysql_select_db(DATABASE);
    }
}
</pre>
<p>
Now that we&#8217;ve connected, we can add the method to add the data into the table that we&#8217;ve created. Add the following into your file:
</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
class Comment {
    private static function connect() {
        mysql_connect(HOST, USERNAME, PASSWORD);
        mysql_select_db(DATABASE);
    }
    public function postData() {
        $this-&gt;connect();
        $email = mysql_real_escape_string($_POST['email']);
        $comment = mysql_real_escape_string($_POST['comment']);
        $insert = mysql_query(&quot;INSERT INTO &quot; . TABLE . &quot; (email, comment) VALUES ('&quot; . $email . &quot;', '&quot; . $comment . &quot;')&quot;);
        if (!$this-&gt;isAjax()) {
            if ($insert) {
                return $this-&gt;displaySuccess();
            }
            return $this-&gt;displayError();
        }
        if ($insert) {
            return &quot;true&quot;;
        }
        return &quot;false&quot;;
    }
}
</pre>
<p>
We&#8217;re using the <code>TABLE</code> constant in this method, which is, again, defined in your <code>conf.php</code> page. You&#8217;ll also notice the <code>isAjax()</code> method that we&#8217;re calling. We&#8217;ll define that next. It will be used to determine if the code is being called via ajax or if the user has JavaScript disabled, and will display the appropriate success or error message. Let&#8217;s add that in now.
</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
class Comment {
    private static function connect() {
        mysql_connect(HOST, USERNAME, PASSWORD);
        mysql_select_db(DATABASE);
    }
    public function postData() {
        $this-&gt;connect();
        $email = mysql_real_escape_string($_POST['email']);
        $comment = mysql_real_escape_string($_POST['comment']);
        $insert = mysql_query(&quot;INSERT INTO &quot; . TABLE . &quot; (email, comment) VALUES ('&quot; . $email . &quot;', '&quot; . $comment . &quot;')&quot;);
        if (!$this-&gt;isAjax()) {
            if ($insert) {
                return $this-&gt;displaySuccess();
            }
            return $this-&gt;displayError();
        }
        if ($insert) {
            return &quot;true&quot;;
        }
        return &quot;false&quot;;
    }
    private static function isAjax() {
        if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) &amp;&amp; strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
          return true;
        }
        return false;
    }
}
</pre>
<p>
The last few methods we need to add into this file are the error and success messages if the user accessed the page without ajax. Your final <code>comment.class.php</code> page should look like the following:
</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
class Comment {
    private static function connect() {
        mysql_connect(HOST, USERNAME, PASSWORD);
        mysql_select_db(DATABASE);
    }
    public function postData() {
        $this-&gt;connect();
        $email = mysql_real_escape_string($_POST['email']);
        $comment = mysql_real_escape_string($_POST['comment']);
        $insert = mysql_query(&quot;INSERT INTO &quot; . TABLE . &quot; (email, comment) VALUES ('&quot; . $email . &quot;', '&quot; . $comment . &quot;')&quot;);
        if (!$this-&gt;isAjax()) {
            if ($insert) {
                return $this-&gt;displaySuccess();
            }
            return $this-&gt;displayError();
        }
        if ($insert) {
            return &quot;true&quot;;
        }
        return &quot;false&quot;;
    }
    private static function isAjax() {
        if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) &amp;&amp; strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
          return true;
        }
        return false;
    }
    private static function displayError() {
        return &quot;Sorry, unable to post your comment. Please try again later.&quot;;
    }
    private static function displaySuccess() {
        return &quot;Thank you! Your comment has been submitted.&quot;;
    }
}
</pre>
<p>
Finally, we need to create one more file in your <code>php</code> folder named <code>comment.php</code>. This is the file that your JavaScript plugin will connect with. Add in the following code:
</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
require_once('conf.php');
require_once('comment.class.php');

$comment = new Comment();
$posts = $comment-&gt;postData();
echo $posts;
</pre>
<p>
The 2 include files are for the configuration file, and the class that we created to add the data into the database. This file uses <code>echo</code> to return a response back to the JavaScript plugin (or display on the screen, if the user has JavaScript disabled). The plugin then displays the error or success message based on the <code>echo</code>.
</p>
<h3>Conclusion</h3>
<p>
So there you have it. Hopefully, you should have a fully functional comment form driven by ajax. If not, retrace your steps from the tutorial or leave a comment and we&#8217;ll get you on the right track. Keep in mind that this is for demonstration purposes only, and you may need to tweak some styles or functionality to make this work with your own personal website. Thanks for reading!
</p>
<p>
<a href="http://web.kingtutz.com/wp-content/plugins/download-monitor/download.php?id=2" title="Downloaded 75 times"><img src="http://web.kingtutz.com/files/2011/08/download.png" alt="Ajax Comment Form" /></a><a href="http://kingtutz.com/demopages/web/ajax-form/index.html" target="_blank"><img src="http://web.kingtutz.com/files/2011/08/demo.png" alt="View the demo" border="0" /></a></p>



		<!-- Added by WP-DragToShare-eXtended Plugin -->
		<script type="text/javascript">
			dtsv.dtse_post_218_permalink = 'http://web.kingtutz.com/2011/08/18/create-an-ajax-driven-comment-form/';
			dtsv.dtse_post_218_title = 'Create an Ajax Driven Comment Form';
		</script>
		<!-- End of WP-DragToShare-eXtended Plugin --><img src="http://feeds.feedburner.com/~r/KingTutzWeb-LearnPhpJavascriptRubyMore/~4/s09JtvUMzZo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://web.kingtutz.com/2011/08/18/create-an-ajax-driven-comment-form/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://web.kingtutz.com/2011/08/18/create-an-ajax-driven-comment-form/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=create-an-ajax-driven-comment-form</feedburner:origLink></item>
		<item>
		<title>The Basics: Building a jQuery Plugin</title>
		<link>http://feedproxy.google.com/~r/KingTutzWeb-LearnPhpJavascriptRubyMore/~3/Q2_91tcnOLk/</link>
		<comments>http://web.kingtutz.com/2011/07/23/the-basics-building-a-jquery-plugin/#comments</comments>
		<pubDate>Sat, 23 Jul 2011 13:56:32 +0000</pubDate>
		<dc:creator>Justin Podzimek</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://web.kingtutz.com/?p=50</guid>
		<description><![CDATA[If you're looking to become a tried-and-true front end developer, the jQuery plugin is probably going to be your best friend. In today's post, we'll go through the basics of creating your very own plugin. Let's get started!]]></description>
			<content:encoded><![CDATA[<h3><span>Step One:</span> Adding the Groundwork</h3>
<p>
The beginning of your plugin should always be the closure. The closure prevents variables from extending into the global scope. We&#8217;ll define the variable <code>$</code> as <code>jQuery</code> to prevent any conflicts that may arise if you ever use the jQuery library with other JavaScript libraries that also utilize the <code>$</code> symbol. If working on a project with a group of other people, it might also be a good idea to pre-define <code>window</code>, <code>document</code>, and <code>undefined</code>, so that you&#8217;re sure what you&#8217;re calling is what you&#8217;re getting. But for now, we&#8217;ll just stick with <code>jQuery</code>.
</p>
<pre class="brush: jscript; title: ; notranslate">
(function($) {

}(jQuery));
</pre>
<h3><span>Step Two:</span> Naming the Plugin</h3>
<p>
Next, we&#8217;ll extend the <code>jQuery.fn</code> object to include our own custom method. Replace <code>nameOfPlugin</code> with the name of your plugin, whatever it may be. In our plugin, we&#8217;ll be passing in some custom options that we&#8217;ll be able to utilize in our plugin. This helps keep the plugin as flexible as you want, so you can reuse it for different projects without re-writing the plugin code.
</p>
<pre class="brush: jscript; title: ; notranslate">
(function($) {
    $.fn.nameOfPlugin = function(opts) {

    };
}(jQuery));
</pre>
<h3><span>Step Three:</span> Defining the Default Variables</h3>
<p>
Now that we have the ability to pass in some optional parameters to our plugins instantiation, we&#8217;re going to want some default values. So we&#8217;ll create an object named <code>defaults</code> that will hold all of the default options that can optionally be overwritten. Finally, we&#8217;ll create one more variable that we&#8217;ll name <code>options</code> that will hold the final options used for the plugin (the defaults merged with the ones overwriting the defaults passed in during the instantiation).
</p>
<pre class="brush: jscript; title: ; notranslate">
(function($) {
    $.fn.nameOfPlugin = function(opts) {
        var defaults = {},
            options = $.extend(defaults, opts);

    };
}(jQuery));
</pre>
<h3><span>Step Four:</span> Looping Through the Objects</h3>
<p>
This next step will loop through all the matching jQuery objects used during the instantiation and run the function you give it. For instance, consider <code>$('div.foo').nameoOfPlugin()</code>. The loop will run over all <code>&lt;div /&gt;</code>&#8216;s with the class name of <code>foo</code>.
</p>
<pre class="brush: jscript; title: ; notranslate">
(function($) {
    $.fn.nameOfPlugin = function(opts) {
        var defaults = {},
            options = $.extend(defaults, opts);
        return this.each(function() {

        });
    };
}(jQuery));
</pre>
<h3><span>Step Five:</span> Make the Magic Happen</h3>
<p>
From here on out, it&#8217;s up to you to add the functionality to make your plugin work. Remember, anything that you include inside the <code>each</code> loop will be iterated over as many jQuery objects are matched during the instantiation. You can use <code>this</code> inside the loop to target the current object, but be mindful of your scope. I like to pre-define a couple variables at the beginning of my loop so I know exactly what I&#8217;m targeting.
</p>
<pre class="brush: jscript; title: ; notranslate">
(function($) {
    $.fn.nameOfPlugin = function(opts) {
        var defaults = {},
            options = $.extend(defaults, opts);
        return this.each(function() {
            var base = this,
                $base = $(this);
        });
    };
}(jQuery));
</pre>
<h3><span>Step Six:</span> Instantiating the Plugin</h3>
<p>
The final step (besides actually writing the plugin functionality) is to instantiate it. At the bottom of your markup file (right before the closing <code>&lt;body&gt;</code> tag, we&#8217;ll add the following code. Notice the continued use of the closure. Again, this is to make sure <code>$</code> always equals <code>jQuery</code>. Replace the selector with what you&#8217;re looking to target, the plugin name with whatever you named your plugin, and you&#8217;re all set.
</p>
<pre class="brush: xml; title: ; notranslate">
&lt;script type=&quot;text/javascript&quot;&gt;
(function($) {
    $('div').nameOfPlugin();
}(jQuery));
&lt;/script&gt;
</pre>
<h3>Conclusion</h3>
<p>
So if you followed the directions above, you should now have the template for your own plugin and you can start creating some professional looking applications. Remember to keep your code as clean and readable as possible (with commented documentation), run it through <a href="http://www.jslint.com/" target="_blank">JSLint</a> to make it sparkle, and minify a copy of it to send to production.
</p>
<p>
If you have any questions, comments, or had any troubles in getting your plugin to work, post them in the comments and we&#8217;ll get things sorted out for you. Thanks for reading, and happy jQuerying!</p>



		<!-- Added by WP-DragToShare-eXtended Plugin -->
		<script type="text/javascript">
			dtsv.dtse_post_50_permalink = 'http://web.kingtutz.com/2011/07/23/the-basics-building-a-jquery-plugin/';
			dtsv.dtse_post_50_title = 'The Basics: Building a jQuery Plugin';
		</script>
		<!-- End of WP-DragToShare-eXtended Plugin --><img src="http://feeds.feedburner.com/~r/KingTutzWeb-LearnPhpJavascriptRubyMore/~4/Q2_91tcnOLk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://web.kingtutz.com/2011/07/23/the-basics-building-a-jquery-plugin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://web.kingtutz.com/2011/07/23/the-basics-building-a-jquery-plugin/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=the-basics-building-a-jquery-plugin</feedburner:origLink></item>
	</channel>
</rss>

