php-код:
Код: Выделить всё
//Retrieve form data.
//GET - user submitted data using AJAX
//POST - in case user does not support javascript, we'll use POST instead
$name = ($_GET['name']) ? $_GET['name'] : $_POST['name'];
$email = ($_GET['email']) ?$_GET['email'] : $_POST['email'];
$message = ($_GET['message']) ?$_GET['message'] : $_POST['message'];
//flag to indicate which method it uses. If POST set it to 1
if ($_POST) $post=1;
//Simple server side validation for POST data, of course, you should validate the email
if (!$name) $errors[count($errors)] = 'Please enter your name.';
if (!$email) $errors[count($errors)] = 'Please enter your email.';
if (!$message) $errors[count($errors)] = 'Please enter your message.';
//If the errors array is empty, send the mail
if (!$errors) {
// ====== Your mail here ====== //
$to = 'alireza.b1993@gmail.com';
// Sender
$from = $name . ' ';
//subject and the html message
$subject = 'Message from your website';
$message = '
Name:' . $name . '
Email:' . $email . '
Message:' . nl2br($message) . '
';
// Send the mail
$result = sendmail($to, $subject, $message, $from);
//if POST was used, display the message straight away
if ($_POST) {
if ($result) echo 'Thank you! We have received your message.';
else echo 'Sorry, unexpected error. Please try again later';
//else if GET was used, return the boolean value so that
//ajax script can react accordingly
//1 means success, 0 means failed
} else {
echo $result;
}
// If the errors array has values
} else {}
// Simple mail function with HTML header
function sendmail($to, $subject, $message, $from) {
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=utf-8" . "\r\n";
$headers .= 'From: ' . $from . "\r\n";
$result = mail($to,$subject,$message,$headers);
if ($result) return 1;
else return 0;
}
?>
Код: Выделить всё
name:
email :*
message :*
Код: Выделить всё
(function() {
var animateSpeed=300;
var emailReg = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
// Validating
function validateName(name) {
if (name.val()=='*') {name.addClass('validation-error',animateSpeed); return false;}
else {name.removeClass('validation-error',animateSpeed); return true;}
}
function validateEmail(email,regex) {
if (!regex.test(email.val())) {email.addClass('validation-error',animateSpeed); return false;}
else {email.removeClass('validation-error',animateSpeed); return true;}
}
function validateMessage(message) {
if (message.val()=='') {message.addClass('validation-error',animateSpeed); return false;}
else {message.removeClass('validation-error',animateSpeed); return true;}
}
$('#send').click(function() {
var result=true;
var name = $('input[name=name]');
var email = $('input[name=email]');
var message = $('textarea[name=message]');
// Validate
if(!validateName(name)) result=false;
if(!validateEmail(email,emailReg)) result=false;
if(!validateMessage(message)) result=false;
if(result==false) return false;
// Data
var data = 'name=' + name.val() + '&email=' + email.val() + '&message=' + encodeURIComponent(message.val());
// Disable fields
$('.text').attr('disabled','true');
// Loading icon
$('.loading').show();
// Start jQuery
$.ajax({
// PHP file that processes the data and send mail
url: "contact.php",
// GET method is used
type: "POST",
// Pass the data
data: data,
//Do not cache the page
cache: false,
// Success
success: function (html) {
if (html==1) {
// Loading icon
$('.loading').fadeOut('slow');
//show the success message
$('.success-message').slideDown('slow');
// Disable send button
$('#send').attr('disabled',true);
}
else {
$('.loading').fadeOut('slow')
alert('Sorry, unexpected error. Please try again later.');
}
}
});
return false;
});
$('input[name=name]').blur(function(){validateName($(this));});
$('input[name=email]').blur(function(){validateEmail($(this),emailReg); });
$('textarea[name=message]').blur(function(){validateMessage($(this)); });
})();
Скачать здесь
Подробнее здесь: https://stackoverflow.com/questions/330 ... ntact-form