我想在我的网站上使用此联系表。但当我点击发送时出现错误。我真的不知道如何解决它,但以我的一点知识,它的 jquery 问题可以通过,所以它出错了。解决它对我来说非常重要。您将需要此代码。谢谢你帮助我,抱歉我的英语不好
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 = '[email protected]';
// Sender
$from = $name . ' <' . $email . '>';
//subject and the html message
$subject = 'Message from your website';
$message = '
<!DOCTYPE html>
<html>
<head></head>
<body>
<table>
<tr><td>Name:</td><td>' . $name . '</td></tr>
<tr><td>Email:</td><td>' . $email . '</td></tr>
<tr><td>Message:</td><td>' . nl2br($message) . '</td></tr>
</table>
</body>
</html>';
// 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;
}
?>
html代码:
<div id="contact-form">
<form method="post" action="contact.php" style="direction:rtl">
<div class="field">
<label>name:</label>
<input type="text" name="name" class="text" />
</div>
<div class="field">
<label>email :<span>*</span></label>
<input type="text" name="email" class="text" />
</div>
<div class="field">
<label>message :<span>*</span></label>
<textarea name="message" class="text textarea" ></textarea>
</div>
<div class="field">
<input type="button" id="send" value="ارسال"/>
<div class="loading"></div>
</div>
</form>
</div>
这是js代码:
(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)); });
})();
如果您想要完整的 html 页面,这是下载链接 在此下载
错误出现在您的 contact.php 代码中。您检查 $errors 是否包含数据,但如果没有错误,则您的数组不存在。
为了进行检查,必须先将数组声明为空:
$errors = array();