为什么这个联系表格不起作用?

时间:2011-03-08 02:43:11

标签: php

尝试进行客户端和服务器端验证我有2个文件。客户端很好,似乎服务器端出错,它根本不发送。

感谢。

form.php的:

<html>
<head>
<style type="text/css">
label.error{color:red; font-weight:bold;}
</style>
<script type="text/javascript" src="jquery.js"> </script>
<script type="text/javascript" src="validation.js"> </script>
<script type="text/javascript"> 
$.validator.setDefaults({
    submitHandler: function() { alert("submitted!"); }
});

$().ready(function() {
    // validate the comment form when it is submitted
    $("#commentForm").validate();

    // validate signup form on keyup and submit
    $("#signupForm").validate({
        rules: {
            firstname: "required",
            lastname: "required",
            username: {
                required: true,
                minlength: 2
            },
            password: {
                required: true,
                minlength: 5
            },
            confirm_password: {
                required: true,
                minlength: 5,
                equalTo: "#password"
            },
            email: {
                required: true,
                email: true
            },
            topic: {
                required: "#newsletter:checked",
                minlength: 2
            },
            agree: "required"
        },
        messages: {
            firstname: "Please enter your firstname",
            lastname: "Please enter your lastname",
            username: {
                required: "Please enter a username",
                minlength: "Your username must consist of at least 2 characters"
            },
            password: {
                required: "Please provide a password",
                minlength: "Your password must be at least 5 characters long"
            },
            confirm_password: {
                required: "Please provide a password",
                minlength: "Your password must be at least 5 characters long",
                equalTo: "Please enter the same password as above"
            },
            email: "Please enter a valid email address",
            agree: "Please accept our policy"
        }
    });

    // propose username by combining first- and lastname
    $("#username").focus(function() {
        var firstname = $("#firstname").val();
        var lastname = $("#lastname").val();
        if(firstname && lastname && !this.value) {
            this.value = firstname + "." + lastname;
        }
    });

    //code to hide topic selection, disable for demo
    var newsletter = $("#newsletter");
    // newsletter topics are optional, hide at first
    var inital = newsletter.is(":checked");
    var topics = $("#newsletter_topics")[inital ? "removeClass" : "addClass"]("gray");
    var topicInputs = topics.find("input").attr("disabled", !inital);
    // show when newsletter is checked
    newsletter.click(function() {
        topics[this.checked ? "removeClass" : "addClass"]("gray");
        topicInputs.attr("disabled", !this.checked);
    });
});
</script> 

</head>

<body>
<form class="cmxform" id="commentForm" method="post" action="f_validate.php" > 
    <fieldset> 
        <legend>Please provide your name provide required fields below.</legend> 
        <p> 
            <label for="cname">* Name</label> 
            <input id="cname" name="name" class="required" minlength="2" /> 
        <p> 
            <label for="cemail">* Email</label> 
            <input id="cemail" name="email" class="required email" /> 
        </p> 
        <p> 
            <label for="curl">URL</label> 
            <input id="curl" name="url" class="url" value="" /> 
        </p> 
        <p> 
            <label for="ccomment">* Comment</label> 
            <textarea id="ccomment" name="comment" class="required"></textarea> 
        </p> 
        <p> 
            <input class="submit" type="submit" value="Submit"/> 
        </p> 
    </fieldset> 
</form> 

</body>

</html>

f_validate.php

<?php 
$errors = '';
$myemail = 'myemail@email.com';
if(empty($_POST['name'])  || 
   empty($_POST['email']) || 
   empty($_POST['comment']))
{
    $errors .= "\n Error: all fields are required";
}

$name = $_POST['name']; 
$email_address = $_POST['email']; 
$comment = $_POST['comment']; 

if (!eregi(
"^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", 
$email_address))
{
    $errors .= "\n Error: Invalid email address";
}

if( empty($errors))
{
    $to = $myemail; 
    $email_subject = "Contact form submission: $name";
    $email_body = "You have received a new message. ".
    " Here are the details:\n Name: $name \n Email: $email_address \n Message \n $comment"; 

    $headers = "From: $myemail"; 
    $headers .= "Reply-To: $email_address";

    mail($to,$email_subject,$email_body,$headers);
    //redirect to the 'thank you' page
    header('Location: http://www.josephdickinson.com');
} 
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
    <title>Contact form handler</title>
</head>

<body>
<!-- This page is displayed only if there is some error -->
<?php
echo nl2br($errors);
?>


</body>
</html>

1 个答案:

答案 0 :(得分:1)

  1. 检查eregi正则表达式是否真的有效(不推荐使用eregi,顺便说一句,你应该切换到preg_match
  2. 检查mail()函数的返回值 - 如果将电子邮件切换到本地SMTP服务器时出现问题,则邮件将返回布尔值FALSE。
  3. 查看SMTP服务器的日志 - 邮件可能卡在队列中,被接收服务器拒绝,转储到垃圾邮件文件夹等等......仅仅因为mail()返回true并不意味着该电子邮件实际上已发送给收件人。