我已经编写了一个关于如何创建ajax联系表单的教程。
这个教程可以在这里使用:
http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/
现在它正在工作,我真的很喜欢它,但由于某种原因,它无法在任何版本的Internet Explorer中工作(在Chrome,Firefox和Opera中测试它很好)。 我真的不知道造成这个问题的原因。
你可以在这里测试联系表格:http://freshbeer.lv/new/latvian/contact.php(它是拉脱维亚语,第一个字段是你的名字,第二个是你的电子邮件,第三个是你的电话(不是必需的),第四个是你的消息)< / p>
以下是此表单的jQuery:
$(function() {
$('.error').hide(); //Hide error message
$(".button").click(function() {
$('.error').hide();
var name = $("input#name").val(); //Check if name is not empty
if (name == "") {
$("label#name_error").show();
$("input#name").focus();
return false;
}
function isValidEmailAddress(emailAddress) { //Function to check e-mail
var pattern = new RegExp(/^(("[\w-+\s]+")|([\w-+]+(?:\.[\w-+]+)*)|("[\w-+\s]+")([\w-+]+(?:\.[\w-+]+)*))(@((?:[\w-+]+\.)*\w[\w-+]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][\d]\.|1[\d]{2}\.|[\d]{1,2}\.))((25[0-5]|2[0-4][\d]|1[\d]{2}|[\d]{1,2})\.){2}(25[0-5]|2[0-4][\d]|1[\d]{2}|[\d]{1,2})\]?$)/i);
return pattern.test(emailAddress);
};
var email = $("input#email").val();//Check email with function
if (!isValidEmailAddress(email)) {
$("label#email_error").show();
$("input#email").focus();
return false;
}
var letter = $("textarea#letter").val(); Check if message was entered
if (letter == "") {
$("label#letter_error").show();
$("textarea#letter").focus();
return false;
}
var dataString = 'name='+ name + '&email=' + email + '&phone=' + phone + '&letter=' + letter; //Build Data String
$.ajax({ //Ajas post data to process.php script
type: "POST",
url: "../scripts/process.php",
data: dataString,
success: function() { //Display success message
$('#contact_form').html("<div id='message'></div>");
$('#message').html("<h2>Contact Form Submitted!</h2>")
.append("<p>We will be in touch soon.</p>")
}
});
return false;
});
});
这里是php(process.php),PHP Mailer用于发送电子邮件
<?php
if ((isset($_POST['name'])) && (strlen(trim($_POST['name'])) > 0)) {
$name = stripslashes(strip_tags($_POST['name']));
} else {$name = 'No name entered';}
if ((isset($_POST['email'])) && (strlen(trim($_POST['email'])) > 0)) {
$email = stripslashes(strip_tags($_POST['email']));
} else {$email = 'No email entered';}
if ((isset($_POST['phone'])) && (strlen(trim($_POST['phone'])) > 0)) {
$phone = stripslashes(strip_tags($_POST['phone']));
} else {$phone = 'No phone entered';}
if ((isset($_POST['letter'])) && (strlen(trim($_POST['letter'])) > 0)) {
$letter = stripslashes(strip_tags($_POST['letter']));
} else {$letter = 'No Message';}
ob_start();
?>
<html>
<head>
<style type="text/css">
</style>
</head>
<body>
<table width="550" border="1" cellspacing="2" cellpadding="2">
<tr bgcolor="#eeffee">
<td>Name</td>
<td><?=$name;?></td>
</tr>
<tr bgcolor="#eeeeff">
<td>Email</td>
<td><?=$email;?></td>
</tr>
<tr bgcolor="#eeffee">
<td>Phone</td>
<td><?=$phone;?></td>
</tr>
<tr bgcolor="#eeeeff">
<td>Message</td>
<td><?=$letter;?></td>
</tr>
</table>
</body>
</html>
<?
$body = ob_get_contents();
require("phpmailer.php");
$mail = new PHPMailer();
$mail->From = "mail@freshbeer.com";
$mail->FromName = "Bryuvers";
$mail->AddAddress("my@email.com","Name 1"); //new mail
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = "New Message!";
$mail->Body = $body;
$mail->AltBody = "Message from website contact form";
if(!$mail->Send()) {
$recipient = 'my@email'; //new mail
$subject = 'Contact form failed';
$content = $body;
mail($recipient, $subject, $content, "From: mail@freshbeer.com\r\nReply-To: $email\r\nX-Mailer: DT_formmail");
exit;
}
?>
这是表格的HTML
<form name="contact" action="">
<fieldset>
<input placeholder="Jusu vards, uzvards (Obligati)" type="text" name="name" id="name" class="text-input" /><br />
<label class="error" for="name" id="name_error"><b>Ludzu noradiet jusu vardu, uzvardu!</b></label><br />
<input placeholder="e-pasta adrese (Obligati)" type="text" name="email" id="email" class="text-input" /><br />
<label class="error" for="email" id="email_error"><b>Ludzu noradiet jusu e-pastu!</b></label><br />
<input placeholder="Telefona Nr." type="text" name="phone" id="phone" class="text-input" /><br /><br />
<textarea placeholder="Jautajums (Obligati)" name="letter" id="letter" class="text-input" /></textarea><br />
<label class="error" for="letter" id="letter_error"><b>Ludzu uzrakstiet jusu jautajumu!</b></label><br />
<input type="submit" name="submit" class="button" id="submit_btn" value="Nosutit" />
</fieldset>
</form>
答案 0 :(得分:2)
javascript变量'phone'未定义。
尝试添加
var phone = $("#phone").val();
编辑:根据建议,直接使用$(“#phone”)
答案 1 :(得分:1)
您在此处有错误:
...
var letter = $("textarea#letter").val(); Check if message was entered
...
应该是
...
var letter = $("textarea#letter").val(); // Check if message was entered
...
并使用更好(更快)的选择器,
...
var letter = $("#letter").val(); // Check if message was entered
...