<?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>Web design blog &#187; ajax</title>
	<atom:link href="http://lastwebdesigner.com/tag/ajax/feed" rel="self" type="application/rss+xml" />
	<link>http://lastwebdesigner.com</link>
	<description>Antonio Fullone &#039;s personal blog about web design</description>
	<lastBuildDate>Sat, 12 Nov 2011 18:39:06 +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>How to send a mail with php ajax and jquery in facebook style</title>
		<link>http://lastwebdesigner.com/featured/how-to-send-a-mail-with-php-ajax-and-jquery-in-facebook-style.html</link>
		<comments>http://lastwebdesigner.com/featured/how-to-send-a-mail-with-php-ajax-and-jquery-in-facebook-style.html#comments</comments>
		<pubDate>Tue, 16 Jun 2009 15:17:56 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[featured]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[Jquery]]></category>
		<category><![CDATA[PhP]]></category>
		<category><![CDATA[web 2.0]]></category>

		<guid isPermaLink="false">http://lastwebdesigner.com/?p=197</guid>
		<description><![CDATA[One of the website that I love is facebook. I&#8217;m not talking about the social network, well I like this social network, but I prefer twitter and linkedin for &#8220;professional use&#8221;, but this is another question, because I&#8217;m talking about the design.:) The use of web 2.0 effects and Ajax make this web site on [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Flastwebdesigner.com%2Ffeatured%2Fhow-to-send-a-mail-with-php-ajax-and-jquery-in-facebook-style.html"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Flastwebdesigner.com%2Ffeatured%2Fhow-to-send-a-mail-with-php-ajax-and-jquery-in-facebook-style.html&amp;style=normal&amp;service=bit.ly&amp;hashtags=ajax,javascript,Jquery,PhP,web+2.0&amp;b=2" height="61" width="50" title="How to send a mail with php ajax and jquery in facebook style" alt=" How to send a mail with php ajax and jquery in facebook style" /><br />
			</a>
		</div>
<p>One of the website that I love is <strong>facebook</strong>. I&#8217;m not talking about the <strong>social network, well I like this social network, but I prefer twitter and linkedin for &#8220;professional use&#8221;, but this is another question, because I&#8217;m talking about</strong> the design.:)</p>
<p>The use of <strong>web 2.0 effects</strong> and <strong>Ajax</strong> make this web site on of my favourites.</p>
<p>With this tutorial I will explain you<span style="text-decoration: underline;"><strong> how to create a message mail system like Facebook. <span id="more-197"></span><br />
</strong></span></p>
<p><span style="text-decoration: underline;"><strong> </strong></span>For make this we need to create some files.</p>
<h4>1. The index file with the mail form.</h4>
<h4>2.The mail.php file that process the form and send the mail, using the simple function mail() of php.</h4>
<h4>3.A css file .</h4>
<h4>4.Jquery, naturally.</h4>
<h4>5.An other Javascript, called Ajax.js, to interact with ajax, php and jquery.</h4>
<p>Surely you can put all in one file, but, for a better understanding and to have a clean code, I prefer to use different files for each one.</p>
<p>Well first of all <a title="send a mail with php jquery and ajax" href="http://lastwebdesigner.com/tests/mail-jquery-php-ajax/">you can see a demo of this page here, </a>for logical reason<span style="text-decoration: underline;"><strong> I&#8217;ve deleted my mail from the source code</strong></span>, / you can use your mail to test the script) this is the only thing that you need to change before use the source code, the rest is ready for use.</p>
<p> <img src='http://lastwebdesigner.com/wp-includes/images/smilies/icon_smile.gif' alt="icon smile How to send a mail with php ajax and jquery in facebook style" class='wp-smiley' title="How to send a mail with php ajax and jquery in facebook style" /> </p>
<p>OK, start with this tutorial.  First we need to create the form page and link to this page the <strong>jquery</strong> and <strong>ajax,js script, </strong>so this is the code :</p>
<blockquote><p><strong>&lt;div&gt;<br />
&lt;div id=&#8221;response&#8221;&gt;</strong></p>
<p><strong>&lt;/div&gt;<br />
&lt;form id=&#8221;formail&#8221; action=&#8221;" method =&#8221;post&#8221;&gt;<br />
&lt;label&gt;Name : &lt;/label&gt;<br />
&lt;input type=&#8221;text&#8221; name=&#8221;name&#8221; id=&#8221;name&#8221; /&gt;<br />
&lt;label&gt;Your mail :&lt;/label&gt;<br />
&lt;input type=&#8221;text&#8221; name=&#8221;mail&#8221; id=&#8221;mail&#8221; /&gt;<br />
&lt;label&gt;Subject : &lt;/label&gt;<br />
&lt;input type=&#8221;text&#8221; name=&#8221;subject&#8221; id=&#8221;subject&#8221; /&gt;<br />
&lt;label&gt;Text :&lt;/label&gt;<br />
&lt;textarea name=&#8221;text&#8221; id=&#8221;text&#8221; cols=&#8221;40&#8243; rows=&#8221;10&#8243;&gt;&lt;/textarea&gt;<br />
&lt;input type=&#8221;submit&#8221; value=&#8221;send mail&#8221; id=&#8221;sendmail&#8221; name=&#8221;sendmail&#8221; /&gt;<br />
&lt;/form&gt;<br />
&lt;/div&gt;</strong></p>
<p><strong>&lt;/div&gt;</strong></p></blockquote>
<p>The <strong>div</strong> called <strong>response</strong>, will be util for the validation and the text message after send the mail( or to get the error if the mail script doesn&#8217;t work), this <strong>div</strong> is really important don&#8217;t forget this.</p>
<p>Now we need to style this, you can style the form as you want, but the only important thing is that the<span style="text-decoration: underline;"> div response must be in display none</span> :</p>
<blockquote><p><strong>#response{display:none;}</strong></p></blockquote>
<p>Is better if you style this and the form, it will look more pretty this is the complete css that I&#8217;ve used :</p>
<blockquote><p><strong>body{<br />
background: #E7E7DC;<br />
font-size: 13px;<br />
font-family: arial;<br />
}<br />
#wrap{<br />
width: 800px;<br />
background: white;<br />
margin: auto;<br />
padding: 10px;<br />
}<br />
#wrap h1{<br />
padding: 10px;<br />
border: 1px solid #ccc;<br />
background: #f8f8f8;<br />
}<br />
.mail{<br />
width:500px;<br />
margin:auto;<br />
}<br />
#formail{<br />
width: 500px;<br />
margin: auto;<br />
}<br />
#formail label{<br />
display: block;<br />
margin: 10px 0;<br />
}<br />
#text{<br />
background: #f8f8f8;<br />
}<br />
#sendmail{<br />
margin-top: 20px;<br />
display: block;<br />
}<br />
#response{<br />
display: none;<br />
border: 1px solid #ccc;<br />
background: #FFFFA0;<br />
padding: 10px;<br />
width: 450px;<br />
}</strong></p></blockquote>
<p>This is the style of the page, now it comes the nice part, the php and the ajax, this will be funny! Let&#8217;s go and create the mail.php page, with this code :</p>
<blockquote><p><strong>&lt;?php<br />
$mail = $_POST['mail'];<br />
$name = $_POST['name'];<br />
$subject = $_POST['subject'];<br />
$text = $_POST['text'];</strong></p>
<p><strong>$to = &#8220;yourmail@domain.com&#8221;;<br />
$message =&#8221; You received  a mail from &#8220;.$mail;<br />
$message .=&#8221; Text of the message : &#8220;.$text;</strong></p>
<p><strong>if(mail($to, $subject,$message)){<br />
echo &#8220;mail successful send&#8221;;<br />
}<br />
else{<br />
echo &#8220;there&#8217;s some errors to send the mail, verify your server options&#8221;;<br />
}<br />
?&gt;</strong></p></blockquote>
<p>Ok, let me explain this:</p>
<p>Firs I create the variables that will contain the froms data values :</p>
<blockquote><p><strong>$mail = $_POST['mail'];<br />
$name = $_POST['name'];<br />
$subject = $_POST['subject'];<br />
$text = $_POST['text'];</strong></p></blockquote>
<p>With the variable<strong> $to</strong>, I store the mail where I want that the mail will be sended( my mail), <span style="text-decoration: underline;">this is the only thing that you must change to make works the script.</span></p>
<p>The <strong>$message</strong> variable get  the <strong>mail</strong> and the text of the <strong>sender</strong>, and put in the body of the mail message.</p>
<p>Finally , with tha <strong>mail() </strong>function from php I send the mail. I&#8217;ve used an if because, <strong>if the mail doesn&#8217;t work you will know. </strong></p>
<p>The mail function is really simple and accept some parameters, the mail where send, the subject and the message of the mail.</p>
<p><a title="mail function php" href="http://es2.php.net/manual/en/function.mail.php">You can read all from the official page of php. </a></p>
<blockquote><p>Now is the time of the<strong> ajax.js.</strong> This is the complete code :</p>
<p><strong>$(document).ready(function(){<br />
$(&#8220;#sendmail&#8221;).click(function(){<br />
var valid = &#8221;;<br />
var isr = &#8216; is required.&#8217;;<br />
var name = $(&#8220;#name&#8221;).val();<br />
var mail = $(&#8220;#mail&#8221;).val();<br />
var subject = $(&#8220;#subject&#8221;).val();<br />
var text = $(&#8220;#text&#8221;).val();<br />
if (name.length&lt;1) {<br />
valid += &#8216;&lt;br /&gt;Name&#8217;+isr;<br />
}<br />
if (!mail.match(/^([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$)/i)) {<br />
valid += &#8216;&lt;br /&gt;A valid Email&#8217;+isr;<br />
}<br />
if (subject.length&lt;1) {<br />
valid += &#8216;&lt;br /&gt;Subject&#8217;+isr;<br />
}<br />
if (text.length&lt;1) {<br />
valid += &#8216;&lt;br /&gt;Text&#8217;+isr;<br />
}<br />
if (valid!=&#8221;) {<br />
$(&#8220;#response&#8221;).fadeIn(&#8220;slow&#8221;);<br />
$(&#8220;#response&#8221;).html(&#8220;Error:&#8221;+valid);<br />
}<br />
else {<br />
var datastr =&#8217;name=&#8217; + name + &#8216;&amp;mail=&#8217; + mail + &#8216;&amp;subject=&#8217; + subject + &#8216;&amp;text=&#8217; + text;<br />
$(&#8220;#response&#8221;).css(&#8220;display&#8221;, &#8220;block&#8221;);<br />
$(&#8220;#response&#8221;).html(&#8220;Sending message &#8230;. &#8220;);<br />
$(&#8220;#response&#8221;).fadeIn(&#8220;slow&#8221;);<br />
setTimeout(&#8220;send(&#8216;&#8221;+datastr+&#8221;&#8216;)&#8221;,2000);<br />
}<br />
return false;<br />
});<br />
});<br />
function send(datastr){<br />
$.ajax({<br />
type: &#8220;POST&#8221;,<br />
url: &#8220;mail.php&#8221;,<br />
data: datastr,<br />
cache: false,<br />
success: function(html){<br />
$(&#8220;#response&#8221;).fadeIn(&#8220;slow&#8221;);<br />
$(&#8220;#response&#8221;).html(html);<br />
setTimeout(&#8216;$(&#8220;#response&#8221;).fadeOut(&#8220;slow&#8221;)&#8217;,2000);<br />
}<br />
});<br />
}</strong></p></blockquote>
<p>What i&#8217;ve done? let me explain :</p>
<p>I suppose that you know the<strong> $(document).ready function of jquery,</strong> I&#8217;ve explained in the previous tutorial, <a title="animated menu jquery" href="http://lastwebdesigner.com/jquery/simple-animated-menu-with-jquery.html">create an animated menu with jquery</a>.</p>
<p>When <strong>#sendmail,</strong> the submit button of the form,  is clicked will start <span style="text-decoration: underline;">the validation of the form,</span> this is important try always to validate your forms, expecially for the <strong>breakballs(usually called spammer&#8230;)</strong>, so the first part of the script validate the form and if on of the input value is under 0 this will stop the script and return an error like in this image</p>
<p><a href="http://lastwebdesigner.com/wp-content/uploads/2009/06/validation.jpg"><img class="alignnone size-full wp-image-198" title="validation" src="http://lastwebdesigner.com/wp-content/uploads/2009/06/validation.jpg" alt="validation How to send a mail with php ajax and jquery in facebook style" width="518" height="191" /></a></p>
<p>the text that appears is stored in the<strong> #response div</strong>, for this reason I said that is important and not only for this. <img src='http://lastwebdesigner.com/wp-includes/images/smilies/icon_smile.gif' alt="icon smile How to send a mail with php ajax and jquery in facebook style" class='wp-smiley' title="How to send a mail with php ajax and jquery in facebook style" /> </p>
<blockquote><p><strong>var valid = &#8221;;<br />
var isr = &#8216; is required.&#8217;;<br />
var name = $(&#8220;#name&#8221;).val();<br />
var mail = $(&#8220;#mail&#8221;).val();<br />
var subject = $(&#8220;#subject&#8221;).val();<br />
var text = $(&#8220;#text&#8221;).val();<br />
if (name.length&lt;1) {<br />
valid += &#8216;&lt;br /&gt;Name&#8217;+isr;<br />
}<br />
if (!mail.match(/^([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$)/i)) {<br />
valid += &#8216;&lt;br /&gt;A valid Email&#8217;+isr;<br />
}<br />
if (subject.length&lt;1) {<br />
valid += &#8216;&lt;br /&gt;Subject&#8217;+isr;<br />
}<br />
if (text.length&lt;1) {<br />
valid += &#8216;&lt;br /&gt;Text&#8217;+isr;<br />
}<br />
</strong></p></blockquote>
<p>To validate the mail, I&#8217;ve used a <strong>regular expression</strong>( <a title="Juan carlos Barreiro" href="http://enterprisedreams.net/">thanks to Juan Carlos</a> <img src='http://lastwebdesigner.com/wp-includes/images/smilies/icon_smile.gif' alt="icon smile How to send a mail with php ajax and jquery in facebook style" class='wp-smiley' title="How to send a mail with php ajax and jquery in facebook style" />  )</p>
<blockquote><p><strong>if (!mail.match(/^([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$)/i))</strong></p></blockquote>
<p>if you try to write <strong>2 mail addres</strong>, this will not works, or if you use white space in the mail field the validation will stop the script. You can try simply leaving white the various fields to understand how it works. All of the error in the validation will appears in the <strong>response div</strong>, that will appear with a<strong> fadeIn effect.</strong></p>
<blockquote><p><strong>if (valid!=&#8221;) {<br />
$(&#8220;#response&#8221;).fadeIn(&#8220;slow&#8221;);<br />
$(&#8220;#response&#8221;).html(&#8220;Error:&#8221;+valid);<br />
}</strong></p></blockquote>
<p>The second part of the script is for create the <span style="text-decoration: underline;"><strong>datastr</strong></span> variable, that will store the value from the form and send in the <strong><span style="text-decoration: underline;">ajax request</span></strong>, and make appear again the <strong>#response div</strong>, yes how I said is important ejejeje, like the <strong>facebook</strong> style, that say that the message is sending.</p>
<blockquote><p><strong>else {<br />
var datastr =&#8217;name=&#8217; + name + &#8216;&amp;mail=&#8217; + mail + &#8216;&amp;subject=&#8217; + subject + &#8216;&amp;text=&#8217; + text;<br />
$(&#8220;#response&#8221;).css(&#8220;display&#8221;, &#8220;block&#8221;);<br />
$(&#8220;#response&#8221;).html(&#8220;Sending message &#8230;. &#8220;);<br />
$(&#8220;#response&#8221;).fadeIn(&#8220;slow&#8221;);<br />
setTimeout(&#8220;send(&#8216;&#8221;+datastr+&#8221;&#8216;)&#8221;,2000);<br />
}</strong></p></blockquote>
<p>In the final  part I&#8217;ve used a <strong>&#8220;return false</strong>&#8220;, so when you click on the submit button this doesn&#8217;t do nothing, well it seems that the button doesn&#8217;t do nothing, but the reason ts that the rest of the work is done by ajax and the function send :</p>
<blockquote><p><strong>function send(datastr){<br />
$.ajax({<br />
type: &#8220;POST&#8221;,<br />
url: &#8220;mail.php&#8221;,<br />
data: datastr,<br />
cache: false,<br />
success: function(html){<br />
$(&#8220;#response&#8221;).fadeIn(&#8220;slow&#8221;);<br />
$(&#8220;#response&#8221;).html(html);<br />
setTimeout(&#8216;$(&#8220;#response&#8221;).fadeOut(&#8220;slow&#8221;)&#8217;,4000);<br />
}<br />
});<br />
}</strong></p></blockquote>
<p>This is the &#8220;<strong>core</strong>&#8221; of the script, this will call th<a title="ajax" href="http://docs.jquery.com/Ajax" target="_blank">e</a><strong><a title="ajax" href="http://docs.jquery.com/Ajax" target="_blank"> $.ajax function of Jquery</a>,</strong> give it the parameters and call the <strong>mail.php</strong> file, finally get the response of the <strong>mail.php</strong>, and return this in the response div, and after<strong> 4 seconds the text fade out</strong>. <img src='http://lastwebdesigner.com/wp-includes/images/smilies/icon_smile.gif' alt="icon smile How to send a mail with php ajax and jquery in facebook style" class='wp-smiley' title="How to send a mail with php ajax and jquery in facebook style" /> </p>
<p>This script is inspired ,how I said, from <strong>facebook message system</strong> and it works at the same way, you can use on your own and change or modify the source code.</p>
<p><a class="demo" title="send a mail with php ajax and jquery" href="http://lastwebdesigner.com/tests/mail-jquery-php-ajax/"><strong>Demo of the script</strong></a></p>
<p><a class="download" title="download the full script" href="http://www.box.net/shared/kklgtosti5"><strong>Download directly the script.</strong></a><br />
I hope is useful , you can use the comments and let me know what do you thing.</p>
<p>If you know something to improve the script, let me know pls.</p>
<p>Bye !!</p>
]]></content:encoded>
			<wfw:commentRss>http://lastwebdesigner.com/featured/how-to-send-a-mail-with-php-ajax-and-jquery-in-facebook-style.html/feed</wfw:commentRss>
		<slash:comments>82</slash:comments>
		</item>
	</channel>
</rss>

