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 google.com 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()
{
//alert(“Hi”);
//name email heading content
var errorJs = “”;
//alert(“name=”+$(‘#name’).val());
var name=$(‘#name’).val();
if($(‘#name’).val()==”")
{
errorJs = ” Error: Name required !! “;
}
if($(‘#email’).val()==”")
{
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!= “”)
{
var eres=email.search(emailFormat);
if(eres == -1)
{
//alert(“Please Enter Valid Email..”);
errorJs+=”n Error: Email not valid. Only valid email required.. !!”;
//document.form1.email.focus();
//return false;
}
}
var subject=$(‘#subject’).val();
if(($(‘#subject’).val()==”") || ($(‘#subject’).val()==”0″))
{
errorJs+= “n Error: Subject required !! “;
}
var content=$(‘#content’).val();
if($(‘#content’).val()==”")
{
errorJs+= “n Error: Content required !! “;
}
if(errorJs != “”)
{
alert(errorJs);
return false;
}
var send=$(‘#send’).val();
//alert(‘name=’+name+’&email=’+email+’&subject=’+subject+’&content=’+content);

$.ajax({
type: ‘POST’,
url: ‘mail.php’,
data: ‘name=’+name+’&email=’+email+’&subject=’+subject+’&content=’+content+’&send=’+send,
success: function(msg) {
//alert(msg);
var emsg = new String(msg);
//alert(emsg);
if (emsg.match(‘error:’))
{
var etmp = emsg.split(‘:’);
//alert(etmp[1]);
$(‘#form1′).fadeIn(200);
$(‘<div id=”returnFromAjax”></div’).insertAfter(‘#returnFromAjax’);
$(‘#returnFromAjax’).html(etmp[1]);
}
else
{
//$(‘#form1′).fadeOut(200);
$(‘#form1′).fadeIn(200);
$(‘<div id=”returnFromAjax”></div’).insertAfter(‘#returnFromAjax’);
$(‘#returnFromAjax’).html(msg);
}
}
});
}
</script>
<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”/>
<br>
<label>Your E-mail:</label><input type=”text” name=”email” id=”email”/>
<br>
<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>
</select>

<br>
<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>
</form>
<div id=”returnFromAjax”>This result return from ajax</div>

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

<?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;
}
if($_POST["send"]==”send”)
{
$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=”";
if(is_valid_email($email))
{

}
else
{
$msg=”error:Valid email required-$email”;
echo $msg;
}
//sending mail start
$to=’jaybharatjay@gmail.com’;
/* 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: jaybharatjay@yahoo.co.inrn”;
$headers .= “Bcc: jaybharatjay@gmail.comrn”;
$maistatus = @mail($to, $subject, $content, $headers);
if($maistatus)
{
echo “success:<br><strong>your message be delivered.</strong>”;
}
else
{
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>”;
}
}
else
{
echo “You can’t access direct this page”;
}
?>

Now you are ready for run it index.php

Share on Facebook