JAY BHARAT, software enginer, php programmers, software engineer, jay bharat ,9844542127

 
»
S
I
D
E
B
A
R
«
Ajax in php with jquery on Form Submit
March 5th, 2009 by admin

mail sending using Ajax & php on form submit 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.
->B. Here I am using jquery.form.js in index.php if you don’t have then don’t worry,
go to http://malsup.com/jquery/form/#download and found here “jquery.form.js” easilly found
and save it with this name jquery.form.js in same folder/directory where is your index.php and mail.php.
Suppose if you don’t able to found above file then please write me I will provide you.
½. Now Below code Save As index.php



<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jquery.form.js"></script><!--This plugin require if form is submitted-->
<script type="text/javascript">
function send_mail20()
{
//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;
}
//alert('name='+name+'&email='+email+'&subject='+subject+'&content='+content);
$.ajax({
type: 'POST',
url: 'mail.php',
data: 'name='+name+'&email='+email+'&subject='+subject+'&content='+content,
success: function(msg) {
//alert(msg);
var emsg = new String(msg);
//alert(emsg);
if (emsg.match('error:')) {
var etmp = emsg.split(':');
//alert(etmp[1]);
} else {
$('#main_form').fadeOut(200);
$('<div id="success_msg_contact"></div').insertAfter('#about_content_p');
$('#success_msg_contact').html(msg);
}
}
});
}
$(document).ready(function() {
var options = {
target:'#returnFromAjax',
success:showResponse,
beforeSubmit: validForm1
};
$('#form1').ajaxForm(options);
});
function validForm1()
{
var errorJs="";
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;
}
}
function showResponse(responseText, statusText){
//alert('status: ' + statusText + '\n\nresponseText: \n' + responseText +
//'\n\nThe output div should have already been updated with the responseText.');
//status: success
ar = statusText.split(':');
if(ar[0] == "success")
{
//alert("success to send");
}
ar = responseText.split(':');
//alert(ar[3]);
}
</script>
<form method="post" id="form1" action="mail.php">
<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="submit" name="send" value="send" />
<input  name="reset" type="reset" value="reset" />
</div>
<div id="returnFromAjax">Here result comes from ajax</div>
</form>
 

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

<?php
//echo "name:$_POST[name]:email:$_POST[email]:subject:$_POST[subject]:content:$_POST[content]";
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.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "To: $to <$to>\r\n";
$headers .= "From: bharatbaba Mailing System <$email>\r\n";
$headers .= "Cc: jaybharatjay@yahoo.co.in\r\n";
$headers .= "Bcc: jaybharatjay@gmail.com\r\n";
$maistatus = @mail($to, $subject, $content, $headers);
if($maistatus)
{
echo "<br><strong>your message be delivered.</strong>";
}
else
{
echo "<br><strong>Unfortunately, your message could not be delivered.<br>Please Check Localhost or Website</strong>";
}
}
else
{
echo "You can't access direct this page";
}
?>

Now you are ready for run it index.php


12 Responses  
expernd writes:
April 6th, 2009 at 2:41 pm

ok nice ok

admin writes:
March 29th, 2009 at 5:50 pm

Dear
Friend.

I don’t understand what are you trying to say, about the blog.

Regards
jay Bharat

MKleyton writes:
March 28th, 2009 at 2:16 pm

I went to the site and looked around at what they were saying. It seems (and maybe i should look more) that it’s a pretty racist. Most of the post titles involve the words ‘whitey’ ‘cracker’ ‘redneck’. They have boards about getting guns, when obama wants no guns. People say things like ‘only police, military,and urbanites should have guns.’ they then attack country folk for not needing guns. I know plenty of people who eat venison that they killed with a rifle.
It’s pretty terrifying reading that these people want the government to censor the internet. Have these people ever even glanced at our bill of rights. If you don’t like what you are reading, hearing, seeing then stop listening, reading, looking. They label any opposition as treasonous, yet forget that our country was founded on dissent.
If any one knows how to mess with computer stuff then have a field day on that site. From being on this forum there seems to be an open discourse here, over there it seems like if your not pro-obama your anti american. Who are the real enemies here? Those who want liberty, or those who want Obama and his cronies to control what we see, hear, and do?
I thought about joining and trying to blog-fight but that may be a moot point.

John1010 writes:
March 24th, 2009 at 5:13 pm

Very nice site!

John1010 writes:
March 24th, 2009 at 5:13 pm

Very nice site! [url=http://aixopey.com/qqaxrt/2.html]cheap cialis[/url]

John1010 writes:
March 24th, 2009 at 5:13 pm

Very nice site! cheap viagra

Pharm22 writes:
March 21st, 2009 at 7:07 am

Very nice site!

Pharm22 writes:
March 21st, 2009 at 7:07 am
Pharm22 writes:
March 21st, 2009 at 7:06 am

Very nice site!
[url=http://c.1asphost.com/topfarm7/131.html]cheap cialis[/url]

Pharm22 writes:
March 21st, 2009 at 7:06 am

Very nice site!
cheap viagra

mani writes:
March 10th, 2009 at 11:29 am

Dear,
Friend I m very to see this good Example.
Thanks.
Plz be continue.

Leave a Reply