0

好的,所以我在我的网站上得到了这个表格,当我测试它并尝试发送一封电子邮件时,它说消息已经发送,但我把它拿到我的电子邮件...

以下是代码:

联系方式.php:

<?php
function ValidateEmail($email)
{
    $regex = '/([a-z0-9_.-]+)'. # name
    '@'. # at
    '([a-z0-9.-]+){1,255}'. # domain & possibly subdomains
    '.'. # period
    "([a-z]+){2,10}/i"; # domain extension 
    if($email == '') {
        return false;
    }
    else {
        $eregi = preg_replace($regex, '', $email);
    }
    return empty($eregi) ? true : false;
}

include 'config.php';

error_reporting (E_ALL ^ E_NOTICE);

$post = (!empty($_POST)) ? true : false;

if($post)
{
    $name = stripslashes($_POST['name']);
    $email = $_POST['email'];
    $subject = stripslashes($_POST['subject']);
    $message = stripslashes($_POST['message']);
    $error = '';
    if(!$name)
    {
        $error .= '<p>Please enter your name.</p>';
    }
    if(!$email)
    {
        $error .= '<p>Please enter an e-mail address.</p>';
    }
    if($email && !ValidateEmail($email))
    {
        $error .= '<p>Please enter a valid e-mail address.</p>';
    }
    if(!$subject || strlen($subject) < 2)
    {
        $error .= "<p>Please enter the subject.</p>";
    }
    if(!$message || strlen($message) < 2)
    {
        $error .= "<p>Please enter your message.</p>";
    }
    if(!$error)
    {
        $mail = mail(WEBMASTER_EMAIL, $subject, $message,
             "From: ".$name." <".$email.">\r\n"
            ."Reply-To: ".$email."\r\n"
            ."X-Mailer: PHP/" . phpversion());
        if($mail)
        {
            echo 'OK';
        }
    }
    else
    {
        echo '<div id="notification">'.$error.'</div>';
    }
}

?>

配置.php:

<?php
// Change this to your email account
define("WEBMASTER_EMAIL", 'my mail address');

?>

表单.js:

$(document).ready(function(){
$("#contactForm").submit(function(){
    var str = $(this).serialize();
    $.ajax({
        type: "POST",
        url: "contact.php",
        data: str,
        success: function(msg){
            $("#note").ajaxComplete(function(event, request, settings){
                if(msg == 'OK')
                {
                    result = '<div id="notification"><p>Your message has been sent. Thank you!</p></div>';
                    $('#contactForm').each (function(){
                        this.reset();
                    });
                }
                else
                {
                    result = msg;
                }
                $(this).html(result);
            });
        }
    });
    return false;
});

});

谢谢!!

4

1 回答 1

0

更改 var str = $(this).serialize(); 数据:$("#contactForm").serialize(),

于 2013-01-29T07:42:10.597 回答