您好 - 我已根据此地址的{tuto}在我的网站上实施了一个联系表单http://designwoop.com/2012/07/tutorial-coding-a-jquery-popup-modal-contact-form/ 它工作正常,唯一的问题是我想在表单中添加一个字段“name”。
所以我在表单的html代码中添加了这行代码:
<input id="name" class="txt" type="name" name="name" placeholder="name"/><br />
修改后的表格代码:
<form id="contact" action="#" method="post" name="contact">
<label for="name">Your Name</label>
<input id="name" class="txt" type="name" name="name" placeholder="name"/><br />
<label for="email">Your E-mail</label>
<input id="email" class="txt" type="email" name="email" placeholder="e-mail address"/>
<br />
<textarea id="msg" class="txtarea" name="msg" placeholder="Type your message here..."></textarea>
<button id="send">Send E-mail</button>
</form>
我还将此添加到发送邮件php文件中:
$msg .= "<p><strong>From:</strong> ".$username."</p>\r\n";
发送邮件php文件的完整代码:
?php
$sendto = "myemail@gmail.com";
$usermail = $_POST['email'];
$content = nl2br($_POST['msg']);
$subject = "New Message frm";
$headers = "From: " . strip_tags($usermail) . "\r\n";
$headers .= "Reply-To: ". strip_tags($usermail) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html;charset=utf-8 \r\n";
$msg = "<html><body style='font-family:Arial,sans-serif;'>";
$msg .= "<h2 style='font-weight:bold;border-bottom:1px dotted #ccc;'>New message</h2>\r\n";
$msg .= "<p><strong>From:</strong> ".$username."</p>\r\n";
$msg .= "<p><strong>E-mail:</strong> ".$usermail."</p>\r\n";
$msg .= "<p><strong>Message:</strong> ".$content."</p>\r\n";
$msg .= "</body></html>";
if(@mail($sendto, $subject, $msg, $headers)) {
echo "true";
} else {
echo "false";
}
?>
最后一部分:联系表单的javascript代码。这是我想我需要添加对我添加的新字段“name”的引用。但不知道在哪里? (并且还要使该字段成为必填项)。非常感谢您的帮助。非常感谢
// JavaScript Document
function validateEmail(email) {
var reg = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return reg.test(email);
}
$(document).ready(function() {
$(".modalbox").fancybox();
$("#contact").submit(function() { return false; });
$("#send").on("click", function(){
var emailval = $("#email").val();
var msgval = $("#msg").val();
var msglen = msgval.length;
var mailvalid = validateEmail(emailval);
if(mailvalid == false) {
$("#email").addClass("error");
}
else if(mailvalid == true){
$("#email").removeClass("error");
}
if(msglen < 4) {
$("#msg").addClass("error");
}
else if(msglen >= 4){
$("#msg").removeClass("error");
}
if(mailvalid == true && msglen >= 4) {
// if both validate we attempt to send the e-mail
// first we hide the submit btn so the user doesnt click twice
$("#send").replaceWith("<em>sending...</em>");
$.ajax({
type: 'POST',
url: 'sendmessage.php',
data: $("#contact").serialize(),
success: function(data) {
if(data == "true") {
$("#contact").fadeOut("fast", function(){
$(this).before("<p><strong>Success! Your message has been sent, thank you.</strong></p>");
setTimeout("$.fancybox.close()", 1000);
});
}
}
});
}
});
});
答案 0 :(得分:0)
将以下代码替换为:
var emailval = $("#email").val();
var msgval = $("#msg").val();
var msglen = msgval.length;
var mailvalid = validateEmail(emailval);
//Get our name
var nameval = $("#name").val();
//Get our name length
var namelen = nameval.length;
然后在
下 if(msglen < 4) {
$("#msg").addClass("error");
}
else if(msglen >= 4){
$("#msg").removeClass("error");
}
添加此
if(namelen < 2) {
//name must be at least 2 characters
$("#name").addClass("error");
}
else if(namelen >= 2){
$("#name").removeClass("error");
}
完成了JavaScript!
对于PHP部分搜索:
$usermail = $_POST['email'];
添加:
$username = $_POST['name'];
试试我的朋友:)