表格不发送

时间:2014-01-22 22:05:21

标签: php forms

我正在为客户端暂存一个网站,我正在尝试从头开始创建表单而不是使用插件。

我不确定我哪里出错了。该页面不断刷新主页,不会发送电子邮件。

有人可以在我的代码中指出我出错的地方......

提前致谢!

<?php
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        $name = $_POST['name'];
        $email = $_POST['email'];
        $message = $_POST['message'];
        $from = 'From: Test'; 
        $to = 'email@example.com'; 
        $subject = 'Hello';

        if ($name == "" OR $email == "") {
            echo "You must specify a value for name, email address, and message.";
            exit;
        }

        foreach( $_POST as $value ){
                if( stripos($value,'Content-Type:') !== FALSE ){
                    echo "There was a problem with the information you entered.";
                    exit;
                }
        }

        require_once("assets/inc/phpmailer/class.phpmailer.php");
        $mail = new PHPMailer();

        if (!$mail->ValidateAddress($email)){
            echo "You must specify a valid email address.";
        }

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

        header("Location: http://natashamcdiarmid.com/clients/JLP/wp/contact/?status=thanks");
        exit;
    }
?>




<?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks") { ?>
    <p>Thanks, I'll get back to your shortly!</p>
<?php } else ?>

<form method="post" action="contact">

    <p><label>Name</label></p>
    <input name="name" placeholder="Type Here">

    <p><label>Email</label></p>
    <input name="email" type="email" placeholder="Type Here">

    <p><label>Message</label></p>
    <textarea name="message" placeholder="Type Here"></textarea>

    <p><label>*What is 2+2?</label></p>
    <input name="human" placeholder="Type Here">

    <p><input id="submit" name="submit" type="submit" value="Submit"></p>

    <?php
        if ($_POST['submit']) {
            if (mail ($to, $subject, $body, $from)) { 
                echo '<p>Your message has been sent!</p>';
            } else { 
                echo '<p>Something went wrong, go back and try again!</p>'; 
            }
        }
    ?>

</form>    

2 个答案:

答案 0 :(得分:0)

您要发布的表单不是目录。

<form method="post" action="contact">  
 should it be contact.php?

此操作应该是表单处理程序的目录

答案 1 :(得分:0)

WordPress总是遇到的一个主要问题是它使用了一些具有通用名称的请求变量,并且弄乱它们会导致不可预测的错误。例如,name参数用于查找和显示当前帖子或网页。

尝试将name字段重命名为其他内容,例如your_name

当我创建在WordPress中使用的表单时,通常我会在每个字段前加上自定义的内容,例如acme_contact_nameacme_contact_email等。多点打字,但更安全。