mail sending using Ajax & php on form click using jquery.
->A. Here I am using jquery.js in index.php if you don’t have then don’t worry,
go to and search ” jquery.js download” easilly found
and save it with this name jquery.js in same folder/directory where is your index.php and mail.php.
½. Now Below code Save As index.php

<script type=”text/javascript” src=”jquery.js”></script>
<script type=”text/javascript”>
function send_mail()
//name email heading content
var errorJs = “”;
var name=$(‘#name’).val();
errorJs = ” Error: Name required !! “;
errorJs+= “n Error: Email required !! “;
var email=$(‘#email’).val();
var emailFormat = /^w(.?[w-])*@w(.?[w-])*.[a-zA-Z]{2,6}(.[a-zA-Z]{2})?$/i;
if(email!= “”)
if(eres == -1)
//alert(“Please Enter Valid Email..”);
errorJs+=”n Error: Email not valid. Only valid email required.. !!”;
//return false;
var subject=$(‘#subject’).val();
if(($(‘#subject’).val()==”") || ($(‘#subject’).val()==”0″))
errorJs+= “n Error: Subject required !! “;
var content=$(‘#content’).val();
errorJs+= “n Error: Content required !! “;
if(errorJs != “”)
return false;
var send=$(‘#send’).val();

type: ‘POST’,
url: ‘mail.php’,
data: ‘name=’+name+’&email=’+email+’&subject=’+subject+’&content=’+content+’&send=’+send,
success: function(msg) {
var emsg = new String(msg);
if (emsg.match(‘error:’))
var etmp = emsg.split(‘:’);
$(‘<div id=”returnFromAjax”></div’).insertAfter(‘#returnFromAjax’);
$(‘<div id=”returnFromAjax”></div’).insertAfter(‘#returnFromAjax’);
<form method=”post” id=”form1″>
<input type=”hidden” name=”send” id=”send” value=”send”>

<label>Your Name:</label><input type=”text” name=”name” id=”name”/>
<label>Your E-mail:</label><input type=”text” name=”email” id=”email”/>
<label>Message Subject:</label>

<select id=”subject” name=”subject”>
<option value=”0″>Choose a subject</option>
<option value=”1″>subject1</option>
<option value=”2″>subject2</option>
<option value=”3″>subject3</option>
<option value=”4″>subject4</option>

<label>Message Content:</label>
<textarea name=”content” id=”content” cols=”25″ rows=”5″></textarea>
<div class=”clear”></div>
<div class=”form-post”>

<!– <input type=”image” src=”<?php echo HOSTURL; ?>/images/buttons/send.png” name=”submit” /> –>
<input type=”button” name=”send” value=”send” onClick=”return send_mail();” />
<input name=”reset” type=”reset” value=”reset” />
<div id=”returnFromAjax”>This result return from ajax</div>

2/2. Now Below code Save As mail.php

//echo “name:$_POST[name]:email:$_POST[email]:subject:$_POST[subject]:content:$_POST[content]:send:$_POST[send]“;
function is_valid_email($email)
$result = TRUE;
if(!eregi(“^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,4})$”, $email))
$result = FALSE;
return $result;
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$content = $_POST['content'];
$name = preg_replace(“/&(^;)+;/”,”",$name);//remove unwanted garbage
$name = preg_replace(“/^s+|s+$/”,”",$name);//remove white space first and last
$email = preg_replace(“/&(^;)+;/”,”",$email);//remove unwanted garbage
$email = preg_replace(“/^s+|s+$/”,”",$email);//remove white space first and last
$subject = preg_replace(“/&(^;)+;/”,”",$subject);//remove unwanted garbage
$subject = preg_replace(“/^s+|s+$/”,”",$subject);//remove white space first and last
$content = preg_replace(“/&(^;)+;/”,”",$content);//remove unwanted garbage
$content = preg_replace(“/^s+|s+$/”,”",$content);//remove white space first and last

$msg=”error:Valid email required-$email”;
echo $msg;
//sending mail start
/* mail sub part start*/
$headers = “MIME-Version: 1.0rn”;
$headers .= “Content-type: text/html; charset=iso-8859-1rn”;
$headers .= “To: $to <$to>rn”;
$headers .= “From: moonreturn Mailing System <$email>rn”;
$headers .= “Cc:”;
$headers .= “Bcc: jaybharatjay@gmail.comrn”;
$maistatus = @mail($to, $subject, $content, $headers);
echo “success:<br><strong>your message be delivered.</strong>”;
echo “error:<br><strong>Unfortunately, your message could not be delivered.<br>Please Check Localhost or Website</strong>”;
//echo “success:<br><strong>your message be delivered.</strong>”;
echo “You can’t access direct this page”;

Now you are ready for run it index.php

Share on Facebook