【问题标题】:php contact form on wordpress site not running correctlywordpress 网站上的 php 联系表未正确运行
【发布时间】:2016-03-08 04:32:46
【问题描述】:

我正在使用 wordpress 安装在 XAMPP 上进行开发。

我的表单看起来不错,一切都是我想要的,但是当我单击提交按钮时,它会加载 index.php 也就是我的网站主页。

所以当我点击提交时,它来自

http://localhost/WP/contact/

http://localhost/WP/contact/index.php

虽然没有任何信息,但它不应该工作......我尝试按照教程进行操作,所以我迷路了,我仍在学习 JS 和 PHP 的基础知识。

这是我的表单代码

<?php
                            if(isset($_POST['submit'])){
                                $name = $_POST['name'];
                                $email = $_POST['email'];
                                $message = $_POST['message'];
                                $from = 'From: $email'; 
                                $to = 'alexgray410@gmail.com'; 
                                $subject = 'Hello';

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

                            <header class="body">

                            </header>

                            <section class="body">
                                <form method="post" action="index.php" class="contact">

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

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

                                    <label class="contact">Message</label>
                                    <textarea name="message" placeholder="Type Here" class="contact"></textarea>
                                    <label class="contact">*What is 2+2? (Anti-spam)</label>
                                    <input name="human" placeholder="Type Here" class="contact">        
                                    <input id="submit" name="submit" type="submit" value="Submit" class="contact">

                                </form>
                            </section>

                        <?php
                        if(isset($_POST['submit'])){
                            $name = $_POST['name'];
                            $email = $_POST['email'];
                            $message = $_POST['message'];
                            $from = 'From: TangledDemo'; 
                            $to = 'demo@tangledindesign.com'; 
                            $subject = 'Hello';
                            $human = $_POST['human'];

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

                            if ($_POST['submit']) {
                                if ($name != '' && $email != '') {
                                    if ($human == '4') {                 
                                        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>'; 
                                    } 
                                } else if ($_POST['submit'] && $human != '4') {
                                    echo '<p>You answered the anti-spam question incorrectly!</p>';
                                }
                                } else {
                                    echo '<p>You need to fill in all required fields!!</p>';
                                }
                            }
                        }
                        ?>

【问题讨论】:

  • 您没有在任何地方验证并返回 false 是我通过浏览代码所理解的..
  • 任何你可以简化或详细说明的方式,对我来说,我不太了解

标签: php wordpress forms contact


【解决方案1】:

您还没有添加标题,请参阅下面的正确 php 邮件格式。

$to=$to_email;
$subject ="Your subject";
$website="example.com";

// Set headers
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: '.$website.'<'.$sender_email. ">\r\n";

// Format message
$contactMessage =
"Your message";
// Send email
$mail_sent=mail($to, $subject, $contactMessage, $headers);

【讨论】:

  • 有什么方法可以帮我简化吗?我是一个真正的新手
  • 在 wordpress 中,您可以尝试使用 contact 7 插件作为联系表格
  • 我喜欢这个的自定义外观。我应该尝试从cs7编辑表单的css吗?我完全按照教程进行,所以我不确定我哪里出错了,但如果你能提供帮助,我真的很感激
【解决方案2】:

我假设该网站在 WordPress 中

if(isset($_POST['submit'])){}的末尾放置php的头部重定向

    <?php
                        if(isset($_POST['submit'])){
                            $name = $_POST['name'];
                            $email = $_POST['email'];
                            $message = $_POST['message'];
                            $from = 'From: TangledDemo'; 
                            $to = 'demo@tangledindesign.com'; 
                            $subject = 'Hello';
                            $human = $_POST['human'];

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

                            if ($_POST['submit']) {
                                if ($name != '' && $email != '') {
                                    if ($human == '4') {                 
                                        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>'; 
                                    } 
                                } else if ($_POST['submit'] && $human != '4') {
                                    echo '<p>You answered the anti-spam question incorrectly!</p>';
                                }
                                } else {
                                    echo '<p>You need to fill in all required fields!!</p>';
                                }
                            }
                        }
header("location:".get_the=permalink(<page_ID_of_CONTACT US page>));
                        ?>
    }

header() 函数应该是最后一个,但在关闭 if 条件之前

【讨论】:

  • 所以我想我可以把它放在 if 语句顶部的位置(在表格开始之前)。但我对代码末尾更复杂的 if 语句的位置感到困惑。还是我只需要放在最后一个部分?
  • 您需要将 header("location:".get_the=permalink()) 放在 if 条件的末尾.. 就在 } 括号之前
  • 不是有三个if条件吗?它需要在所有这些的末尾进行,还是只在最后一个?抱歉,我没有完全理解你的意思
  • 谢谢老兄,我吃完早餐再试试。如果我无法获得它,我可能会尝试使用插件
  • 当我插入该代码时,它会给我一个 500 内部服务器错误。看来我必须先学习 php,然后才能使用此示例,并改用插件。这是一种耻辱,因为我真的很喜欢这种形式的自定义背景。也许我可以从插件或其他东西编辑另一个表单的css
猜你喜欢
  • 1970-01-01
  • 2015-10-17
  • 2022-08-19
  • 2012-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多