1

嗨 - 我已经根据这个地址http://designwoop.com/2012/07/tutorial-coding-a-jquery-popup-modal-contact-form/上的教程在我的网站上实现了一个联系表单 它工作正常,唯一的问题是我想在表单中添加一个字段“名称”。

于是我在表单的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 代码。这就是我想我需要添加对我添加的新字段“名称”的引用的地方。但不知道在哪里?(并且还使该字段成为强制性的)。您的帮助将不胜感激。非常感谢

// 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);
                                                        });
                                                }
                                        }
                                });
                        }
                });
        });
4

1 回答 1

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'];

试试我的朋友:)

于 2012-08-02T18:37:42.443 回答