Wordpress:联系表单不发送和返回错误的页面

时间:2016-09-28 14:39:47

标签: php wordpress forms contact

我正在处理我自己的wordpress主题并且我正在制作联系表单,但它不会向我发送邮件,当我发布表单时,页面会发疯并说它无法找到页面

my contact page

PHP     

    $body = "From: $name\n E-Mail: $email\n Message:\n $message";

    // Check if name has been entered
    if (!$_POST['name']) {
        $errName = 'Please enter your name';
    }

    // Check if email has been entered and is valid
    if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
        $errEmail = 'Please enter a valid email address';
    }

    //Check if message has been entered
    if (!$_POST['message']) {
        $errMessage = 'Please enter your message';
    }
    //Check if simple anti-bot test is correct
    if ($human !== 5) {
        $errHuman = 'Your anti-spam is incorrect';
    }

// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
    if (wp_mail ($to, $subject, $body, $from)) {
        $result='<div class="alert alert-success">Thank You! I will be in touch</div>';
    } else {
        $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
    }
}
    }
?>

HTML

<form class="form-horizontal" role="form" method="post" action="">
                            <div class="form-group">
                                <label for="name" class="col-lg-2 inputName">Name</label>
                                <div class="col-sm-8">
                                    <input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" value="<?php echo htmlspecialchars($_POST['name']); ?>">
                                    <?php echo "<p class='text-danger'>$errName</p>";?>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="email" class="col-lg-2 inputName">Email</label>
                                <div class="col-sm-8">
                                    <input type="email" class="form-control" id="email" name="email" placeholder="example@domain.com" value="<?php echo htmlspecialchars($_POST['email']); ?>">
                                    <?php echo "<p class='text-danger'>$errEmail</p>";?>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="message" class="col-lg-2 inputName">Message</label>
                                <div class="col-sm-8">
                                    <textarea id="inputMessage" class="form-control" rows="4" name="message"><?php echo htmlspecialchars($_POST['message']);?></textarea>
                                    <?php echo "<p class='text-danger'>$errMessage</p>";?>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="human" class="col-lg-2 inputName">2 + 3 = ?</label>
                                <div class="col-sm-8">
                                    <input type="text" class="form-control" id="human" name="human" placeholder="Your Answer">
                                    <?php echo "<p class='text-danger'>$errHuman</p>";?>
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="col-sm-8 col-lg-offset-2">
                                    <input id="submit" name="submit" type="submit" value="Send" class="btn btn-default">
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="col-sm-8 col-lg-offset-2">
                                    <?php echo $result; ?>  
                                </div>
                            </div>
                        </form> 

0 个答案:

没有答案