<?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>WP-Themix &#187; contact form</title>
	<atom:link href="http://www.wp-themix.org/tag/contact-form/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.wp-themix.org</link>
	<description></description>
	<lastBuildDate>Mon, 02 Jan 2012 15:56:48 +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>How to add a jquery/ajax contact form to Wordpress</title>
		<link>http://www.wp-themix.org/wordpress/how-to-add-a-jquery-ajax-contact-form-to-wordpress/</link>
		<comments>http://www.wp-themix.org/wordpress/how-to-add-a-jquery-ajax-contact-form-to-wordpress/#comments</comments>
		<pubDate>Mon, 15 Nov 2010 20:57:57 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[wordpress]]></category>
		<category><![CDATA[ajax contact form]]></category>
		<category><![CDATA[contact form]]></category>
		<category><![CDATA[wordpress ajax contact]]></category>
		<category><![CDATA[wordpress contact form]]></category>
		<category><![CDATA[wordpress jquery contact form]]></category>

		<guid isPermaLink="false">http://www.wp-themix.org/?p=128</guid>
		<description><![CDATA[Did you ever wanted to add a custom contact form to your Wordpress Theme? In this tutorial we will show you step by step how to easily add your own jquery/ajax featured contact form to your Wordpress installation. We will add some default styling which can be adopted and customized by yourself.

1. Adding a page [...]]]></description>
			<content:encoded><![CDATA[<p>Did you ever wanted to add a custom contact form to your Wordpress Theme? In this tutorial we will show you step by step how to easily add your own jquery/ajax featured contact form to your Wordpress installation. We will add some default styling which can be adopted and customized by yourself.</p>
<p><a href="http://dev.wp-themix.org/?page_id=235" target="_blank" title="jQuery Ajax contact form for wordpress"><img src="http://www.wp-themix.org/wp-content/themes/wp-mag/images/demo_content.jpg" alt="demo"></a></p>
<h3>1. Adding a page template</h3>
<p>Did you ever add a custom page template to one of your Wordpress Themes? No? It is really easy. The code below shows you how to do. We would advise you to copy your template&#8217;s page.php file and then copy the code from below and <em>paste it in the content-div of your page.php copy</em>. At the top of your file (name it contact.php f.e.) add the following (Before the get_header and everything else)</p>
<p>
<pre class="brush: php;">&lt;br /&gt;
/*&lt;br /&gt;
Template Name: Contact Form&lt;br /&gt;
*/&lt;br /&gt;
</pre>
</p>
<p>This code makes sure, that you can choose the page template, when adding a new page in your wordpress administration area. Now add the following code to your contact.php&#8217;s content-div:</p>
<p>
<pre class="brush: xml;">&lt;/p&gt;
&lt;div class=&quot;contact&quot;&gt;
&lt;div id=&quot;node&quot;&gt;&lt;/div&gt;
&lt;div id=&quot;success&quot;&gt;Thank you for leaving a message.&lt;/div&gt;
&lt;form name=&quot;contact_form&quot; id=&quot;contact_me&quot;/&gt;
        &lt;label for=&quot;name&quot;&gt;Your Name*&lt;/label&gt;&lt;/p&gt;
&lt;input type=&quot;text&quot; name=&quot;name&quot; value=&quot;&quot; /&gt;
        &lt;label for=&quot;company&quot;&gt;Your Company&lt;/label&gt;&lt;/p&gt;
&lt;input type=&quot;text&quot; name=&quot;company&quot; value=&quot;&quot; /&gt;
        &lt;label for=&quot;telephone&quot;&gt;Telephone Number&lt;/label&gt;&lt;/p&gt;
&lt;input type=&quot;text&quot; name=&quot;telephone&quot; value=&quot;&quot; /&gt;
        &lt;label for=&quot;mail&quot;&gt;Your E-Mail*&lt;/label&gt;&lt;/p&gt;
&lt;input type=&quot;text&quot; name=&quot;mail&quot; value=&quot;&quot; /&gt;
        &lt;label for=&quot;message&quot;&gt;Your Message*&lt;/label&gt;&lt;br /&gt;
        &lt;textarea name=&quot;message&quot;&gt;&lt;/textarea&gt;&lt;/p&gt;
&lt;input type=&quot;submit&quot; value=&quot;submit&quot; /&gt;
    &lt;/form&gt;
&lt;/div&gt;
&lt;p&gt;</pre>
</p>
<p>Allright! Now save your file and upload it to your wordpress-theme directory. After that, add a new page and choose &#8220;Contact Form&#8221; as your page template!</p>
<h3>2. Adding the CSS to your syle.css</h3>
<p>Now that we did the structuring, we need to style the html. To do so, copy the code from below and paste it in your theme&#8217;s style.css. Of course you can customize the whole CSS to adopt your theme&#8217;s look!</p>
<p>
<pre class="brush: css;">&lt;br /&gt;
.contact {&lt;br /&gt;
float: left;&lt;br /&gt;
width: 100%;&lt;br /&gt;
}&lt;br /&gt;
.contact label {&lt;br /&gt;
float: left;&lt;br /&gt;
width: 150px;&lt;br /&gt;
margin-bottom: 10px;&lt;br /&gt;
clear: both;&lt;br /&gt;
}&lt;/p&gt;
&lt;p&gt;.contact input {&lt;br /&gt;
float: left;&lt;br /&gt;
width: 180px;&lt;br /&gt;
border: 1px solid #CCC;&lt;br /&gt;
padding: 3px;&lt;br /&gt;
margin-bottom: 10px;&lt;br /&gt;
}&lt;br /&gt;
.contact textarea {&lt;br /&gt;
float: left;&lt;br /&gt;
width: 250px;&lt;br /&gt;
height: 100px;&lt;br /&gt;
border: 1px solid #CCC;&lt;br /&gt;
}&lt;br /&gt;
.contact #node {&lt;br /&gt;
float: left;&lt;br /&gt;
width: 360px;&lt;br /&gt;
display: none;&lt;br /&gt;
background-color: #CC5553;&lt;br /&gt;
padding: 5px;&lt;br /&gt;
color: #FFF;&lt;br /&gt;
border: 1px solid #9B413F;&lt;br /&gt;
margin-bottom: 10px;&lt;br /&gt;
}&lt;br /&gt;
.contact #success {&lt;br /&gt;
float: left;&lt;br /&gt;
width: 360px;&lt;br /&gt;
background-color: #8ACC39;&lt;br /&gt;
color: #FFF;&lt;br /&gt;
padding: 5px;&lt;br /&gt;
border: 1px solid #6DA32C;&lt;br /&gt;
display: none;&lt;br /&gt;
margin-bottom: 10px;&lt;br /&gt;
}&lt;br /&gt;
.contact input[type=submit] {&lt;br /&gt;
float: left;&lt;br /&gt;
width: auto;&lt;br /&gt;
padding: 5px 20px;&lt;br /&gt;
background-color: #FFF;&lt;br /&gt;
border: 1px solid #CCC;&lt;br /&gt;
text-align: center;&lt;br /&gt;
margin-top: 10px;&lt;br /&gt;
cursor: pointer;&lt;br /&gt;
clear: both;&lt;br /&gt;
margin-left: 150px;&lt;br /&gt;
}&lt;br /&gt;
</pre>
</p>
<h3>3. Adding the Javascript/Ajax Function</h3>
<p>Now we will have to add some jQuery Code to your theme, to make the contact form work! You will need to add the following code to a new file (we name it contact.js).</p>
<p>
<pre class="brush: jscript;">&lt;br /&gt;
jQuery(document).ready(function($) {&lt;br /&gt;
       jQuery(&quot;#contact_me&quot;).submit(function() {&lt;br /&gt;
	var str = jQuery(this).serialize();&lt;br /&gt;
	jQuery.ajax({&lt;br /&gt;
	    type: &quot;POST&quot;,&lt;br /&gt;
	    url: &quot;/wp-admin/admin-ajax.php&quot;,&lt;br /&gt;
	    data: 'action=contact_form&amp;'+str,&lt;br /&gt;
	    success: function(msg) {&lt;br /&gt;
		jQuery(&quot;#node&quot;).ajaxComplete(function(event, request, settings){&lt;br /&gt;
                if(msg == 'sent') {&lt;br /&gt;
                    jQuery(&quot;.contact #node&quot;).hide();&lt;br /&gt;
		    jQuery(&quot;.contact #success&quot;).fadeIn(&quot;slow&quot;);&lt;br /&gt;
                }&lt;br /&gt;
                else {&lt;br /&gt;
                    result = msg;&lt;br /&gt;
                    jQuery(&quot;.contact #node&quot;).html(result);&lt;br /&gt;
		    jQuery(&quot;.contact #node&quot;).fadeIn(&quot;slow&quot;);&lt;br /&gt;
		}&lt;br /&gt;
		});&lt;br /&gt;
	    }&lt;br /&gt;
	});&lt;br /&gt;
	return false;&lt;br /&gt;
    });&lt;br /&gt;
});&lt;br /&gt;
</pre>
</p>
<p>Now that we have the new file &#8220;contact.js&#8221;, upload it to your theme directory.</p>
<h3>4. Handling the Ajax Post-Values </h3>
<p>Now that we have the javascript, we will need to handle the values which are sent by the form. Now it&#8217;s a little tricky because we are using Wordpress-built-in Ajax function. Open up your theme&#8217;s functions.php (if doesn&#8217;t exist add a new file) and paste the following code:</p>
<p>
<pre class="brush: php;">&lt;br /&gt;
add_action('init', 'add_contact_script');&lt;br /&gt;
function add_contact_script() {&lt;br /&gt;
    wp_register_script('contact', get_bloginfo('template_directory') . '/contact.js', array('jquery'), '1.0' );&lt;br /&gt;
    wp_enqueue_script('contact');&lt;br /&gt;
}&lt;br /&gt;
function ajax_contact() {&lt;br /&gt;
    if(!empty($_POST)) {&lt;br /&gt;
	$name = $_POST['name'];&lt;br /&gt;
	$company = $_POST['company'];&lt;br /&gt;
	$telephone = $_POST['telephone'];&lt;br /&gt;
	$mail = $_POST['mail'];&lt;br /&gt;
	$admin_mail = get_bloginfo('admin_email');&lt;br /&gt;
	$msg = $_POST['message'];&lt;br /&gt;
	$error = &quot;&quot;;&lt;br /&gt;
	if(!$name) {&lt;br /&gt;
	    $error .= &quot;Please tell us your name&lt;br/&gt;&quot;;&lt;br /&gt;
	}&lt;br /&gt;
	if(!$mail) {&lt;br /&gt;
	    $error .= &quot;Please tell us your E-Mail address&lt;br/&gt;&quot;;&lt;br /&gt;
	}&lt;br /&gt;
	if(!$msg) {&lt;br /&gt;
	    $error .= &quot;Please add a message&quot;;&lt;br /&gt;
	}&lt;br /&gt;
	if(empty($error)) {&lt;br /&gt;
	    $subject = &quot;New contact form received&quot;;&lt;br /&gt;
	    $message = &quot;You've received a new contact form. \n\n&lt;br /&gt;
			Name: &quot;.$name.&quot;\n&lt;br /&gt;
			Company: &quot;.$company.&quot;\n&lt;br /&gt;
			Telephone: &quot;.$telephone.&quot;\n&lt;br /&gt;
			Mail: &quot;.$mail.&quot;\n&lt;br /&gt;
			Message: &quot;.$msg.&quot;\n&quot;;&lt;br /&gt;
	    $mail = mail($admin_mail, $subject, $message,&lt;br /&gt;
	    &quot;From: &quot;.$name.&quot; &lt;&quot;.$mail.&quot;&gt;rn&quot;&lt;br /&gt;
	    .&quot;Reply-To: &quot;.$mail.&quot;rn&quot;&lt;br /&gt;
	    .&quot;X-Mailer: PHP/&quot; . phpversion());&lt;br /&gt;
	    if($mail) {&lt;br /&gt;
		echo &quot;sent&quot;;&lt;br /&gt;
	    }&lt;br /&gt;
	    die();&lt;br /&gt;
	} else {&lt;br /&gt;
	    echo $error;&lt;br /&gt;
	    die();&lt;br /&gt;
	}&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
add_action('wp_ajax_nopriv_contact_form', 'ajax_contact');&lt;br /&gt;
</pre>
</p>
<p>Make sure that your theme has already included the jQuery Framework, otherwise it won&#8217;t work!</p>
<p>Now that you have all the files uploaded and updated you can have a look at your new contact page and try to write a new contact request. Hope this tutorial was helpful for you! Build your own jquery ajax contact form in Wordpress in just 4 steps!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.wp-themix.org/wordpress/how-to-add-a-jquery-ajax-contact-form-to-wordpress/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>
