【问题标题】:PHP Contact Form FailingPHP联系表格失败
【发布时间】:2017-06-25 10:04:37
【问题描述】:

我想知道这个联系表单出了什么问题。我不知道为什么,但它总是默认为else 并说Something went wrong, please try again later. 我不知道为什么,并且error_log 没有显示任何内容。有什么明显的我想念的吗?我对 PHP 很陌生。

PHP

<?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $message = $_POST['message'];
    $captcha = $_POST['captcha'];
    $from = 'From: HankSmith.com Contact Form'; 
    $to = 'thatbraxjohnsonguy@gmail.com'; 
    $subject = 'HANK SMITH CONTACT FORM';

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

    if ($_POST['submit'] and $captcha == 4) {                
        if (mail ($to, $subject, $body, $from)) { 
        echo '<p style="margin-top: 150; text-align:center; font-size: 18px;">Your message has been sent! Click <a href="../index.php">here</a> to return to the website.</p>';
    } else { 
        echo '<p>Something went wrong, try again later!</p>'; 
    }
}
?>

HTML 表单

            <form class="contactform" method="post" action="php/contact.php">
                <h3>Name</h3>
                <input class="form inputboxes" type="text" name="name">
                <h3>Email</h3>
                <input class="form inputboxes" type="text" name="email">
                <h3>Phone</h3>
                <input class="form inputboxes" type="text" name="phone">
                <h3>Message</h3>
                <textarea class="form inputboxes" name="message"></textarea>
                <h3 class="captchastyle">Are you real? What is 2 + 2?</h3><input class="captcha captchastyle" type="text" name="captcha" maxlength="1">
                <input class="form submit" name="submit" type="submit" value="Submit">
            </form>

【问题讨论】:

  • 可能需要看表格
  • 我会用表格更新帖子。
  • 发布 html 表单。
  • @RodrigoSartoriJarouche 完成。

标签: php html css forms contact


【解决方案1】:

根据您的情况使用isset

if (isset($_POST['submit']) &amp;&amp; $captcha == 4)

它检查该字段是否为非空以及表单是否已提交。

这里&amp;&amp;and 更好看这里的原因:https://stackoverflow.com/a/11861068

【讨论】:

  • 仍然向我抛出同样的问题,我想知道是否只是服务器的传播速度不如我更新的速度。
  • 好的,使用&amp;&amp; 并关闭},如下一个答案中所述。它应该可以工作。
  • 好吧,我只是得到一个空白页而不是错误消息,所以我考虑了这个进度。
  • 现在它杀死了页面哈哈。我会继续解决问题的!
  • 不,我的意思是在@daniel 的回答之后
【解决方案2】:

当我将它复制到我的文本编辑器中时,它引发了解析错误,因为 if (mail)... 部分中的 echo 语句没有右大括号。我从不使用'and',我通常使用'&&',但在这种情况下实际上似乎并不重要。

if ($_POST['submit'] && $captcha == 4) {                
        if (mail ($to, $subject, $body, $from)) { 
        echo '<p style="margin-top: 150; text-align:center; font-size: 18px;">Your message has been sent! Click <a href="../index.php">here</a> to return to the website.</p>';
        } // THIS MUST BE HERE
    } else { 
        echo '<p>Something went wrong, try again later!</p>'; 
    }

此外,您可能希望 if (mail)... 语句具有“else”条件。如果您看到一个空白页面,那是因为您没有处理如果 mail() 函数返回 false 会发生什么:

if (mail ($to, $subject, $body, $from)) { 
  echo '<p style="margin-top: 150; text-align:center; font-size: 18px;">Your message has been sent! Click <a href="../index.php">here</a> to return to the website.</p>';
} else {
  echo '<p>Problem sending mail!';
}

老实说,原生 PHP 的 mail() 函数很糟糕!考虑改用SwiftMailerPHPMailer

【讨论】:

  • 我忘记带牙套了??图。谢谢。我会测试一下。
  • 好的,所以它没有给我一个错误,只是一个空白页。进步!
  • 是的,实际上没有必要,因为您只在 if 条件中运行一个语句。但是,因为你有一个左大括号,所以右大括号是必要的。
  • 您很可能会得到一个空白页,因为 mail() 函数返回 false,请参阅我对答案的编辑以获取解决方案。
  • 你完全正确。现在它正在抛出Problem sending mail. 我将编辑邮件功能。感谢您的帮助!
猜你喜欢
  • 2018-02-08
  • 2020-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-28
相关资源
最近更新 更多