1

我在(单页业务/投资组合)网站底部有一个联系表。

我的 DOCTYPE 上面的脚本如下所示。

<?php
//If the form is submitted
if(isset($_POST['submit'])) {

    //Check to make sure that the name field is not empty
    if(trim($_POST['contactname']) == '') {
        $hasError = true;
    } else {
        $name = trim($_POST['contactname']);
    }

    //Check to make sure that the subject field is not empty
    if(trim($_POST['subject']) == '') {
        $hasError = true;
    } else {
        $subject = trim($_POST['subject']);
    }

    //Check to make sure sure that a valid email address is submitted
    if(trim($_POST['email']) == '')  {
        $hasError = true;
    } else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$",   trim($_POST['email']))) {
        $hasError = true;
    } else {
        $email = trim($_POST['email']);
    }

    //Check to make sure comments were entered
    if(trim($_POST['comment']) == '') {
        $hasError = true;
    } else {
        if(function_exists('stripslashes')) {
            $comments = stripslashes(trim($_POST['message']));
        } else {
            $comments = trim($_POST['message']);
        }
    }

    //If there is no error, send the email
    if(!isset($hasError)) {
        $emailTo = 'myemail@gmail.com'; //Put your own email address here
        $body = "Name: $name \n\nEmail: $email \n\nSubject: $subject \n\nComments:\n $comments";
        $headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;

        mail($emailTo, $subject, $body, $headers);
        $emailSent = true;
    }
}
?>

JQuery 验证确实有效。

<script type="text/javascript">
$(document).ready(function(){
    $("form").validate();
});
</script>

联系表格:

<div class="grid_8">
    <?php if(isset($hasError)) { //If errors are found ?>
        <p class="error">Please check if you've filled all the fields with valid information. Thank you.</p>
    <?php } ?>

    <?php if(isset($emailSent) && $emailSent == true) { //If email is sent ?>
        <p><strong>Email Successfully Sent!</strong></p>
        <p>Thank you <strong><?php echo $name;?></strong> for using my contact form! Your email was successfully sent and I will be in touch with you soon.</p>
    <?php } ?>
    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> 
        <div> 
            <label for="name">Your Name:</label> 
            <div> 
                <input type="text" name="contactname" class="required" /> 
            </div> 
        </div> 
        <div> 
            <label for="email">Your Email:</label> 
            <div> 
                <input type="text" name="email" class="required email" /> 
            </div>   
        </div> 
        <div> 
            <label for="subject">Subject:</label> 
            <div> 
                <input type="text" name="subject" class="required" /> 
            </div>
        </div> 
        <div> 
            <label for="comments">Comments:</label> 
            <div> 
                <textarea name="comment" name="comment" class="required"></textarea> 
            </div> 
        </div> 
        <div> 
            <input id="button" type="submit" value="SEND" /> 
        </div> 
    </form>
    </div>

当您提交表单时,电子邮件不会被发送,也不会来自 php.ini 的任何验证。我究竟做错了什么?

4

2 回答 2

2

当您的表单中isset($_POST['submit'])没有字段时,我不明白为什么应该返回 true。name="submit"

我更喜欢为所有需要的数据使用一个数组:

<!-- ... -->
<input type="text" name="mail[contactname]" class="required" /> 
<!-- ... -->
<input type="text" name="mail[email]" class="required email" /> 
<!-- ... -->
<input type="text" name="mail[subject]" class="required" /> 
<!-- ... -->
<textarea name="comment" name="mail[comment]" class="required"></textarea> 
<!-- ... -->

然后要求那个数组

if (isset($_POST['mail']))
  // ...
于 2011-03-04T15:38:52.160 回答
2

一个快速明显的问题,尽管我有许多不太重要的问题。简单地说,你正在检查$_POST['submit']put there 有name="submit",您正在检查表单中

所以改变:

 <input id="button" type="submit" value="SEND" />

至:

 <input id="button" type="submit" name="submit" value="SEND" />

或更改:

 if(isset($_POST['submit']))

至:

 if(count($_POST))
 // OR
 if(!empty($_POST))

任何一个都可以解决您的问题

于 2011-03-04T15:39:01.810 回答