【问题标题】:Custom Form in WordPress Page Won't Submit/RefreshWordPress 页面中的自定义表单不会提交/刷新
【发布时间】:2018-02-18 10:00:17
【问题描述】:

我有一个使用自定义 HTML/CSS/JS 构建的普通网站。我将站点更改为 WordPress 站点(并将我的自定义 html/css/js 变为自定义 WordPress 主题)。我在该网站的主页上有一个联系表格,当我更改为 WordPress 时它就坏了。

当表单运行时,它会刷新页面并让用户回到表单(使用锚 id=contactphilly),然后用户会看到一条消息,确认他们的电子邮件已发送。

我做了很多谷歌搜索,我认为这与表单中的“操作”选项有关,但无论我尝试将其更改为什么,它都不起作用。我尝试了以下操作值:

<?php the_permalink(); ?>/#contactphilly
#contactphilly
<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>
$_SERVER['REQUEST_URI']

它们都不起作用。

这是表单的代码:

<?php
      if (isset($_POST["submit"])) {
        $name = $_POST['name'];
        $subject = $_POST['subject'];
        $email = $_POST['email'];
        $message = $_POST['message'];
        $from = 'WebKeep.com'; 
        $to = 'info@webkeepteam.com'; 
        $subjectemail = 'Message from WebKeepTeam.com Contact Form ';

    $body = "From: $name\n E-Mail: $email\n Subject: $subject\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';
    }

// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage) {
  if (mail ($to, $subjectemail, $body, $from)) {
    $result='<div>Thank You! We will respond shortly.</div>';
  } else {
    $result='<div>Sorry there was an error sending your message. Please try again later</div>';
  }
}
}
?>

    <form name="contactform" method="post" action="#contactphilly">
    <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">

  <fieldset class="form-group">
    <input type="text" value="<?php echo $name;?>" class="form-control transparent-input" id="name" name="name" placeholder="Name">
  </fieldset>
    <?php echo "<p class='text-danger'>$errName</p>";?>
        <fieldset class="form-group">
    <input type="email" value="<?php echo $email;?>"  class="form-control transparent-input" id="email" name="email" placeholder="Email">
  </fieldset>
    <?php echo "<p class='text-danger'>$errEmail</p>";?>
    <fieldset class="form-group">
    <input type="text" class="form-control transparent-input"  value="<?php echo $subject;?>" id="subject" name="subject" placeholder="Subject">
  </fieldset>
</div>
    <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
  <fieldset class="form-group">
    <textarea class="form-control  transparent-input" id="message"  value="<?php echo $message;?>" name="message" rows="6" placeholder="Message"><?php echo htmlspecialchars($_POST['message']);?></textarea>
  </fieldset>
    <?php echo "<p class='text-danger'>$errMessage</p>";?>
  <input id="submit" name="submit" type="submit" value="Send" class="btn btn-index pull-right">
</div>
    <?php echo $result; ?>
</form>

【问题讨论】:

  • 为什么你不使用初始化操作?您是 wordpress 新手吗?
  • 到底是什么问题?您的表单是否未提交 完成后它只是没有滚动回锚点吗?我还建议您查看如何为 Wordpress 开发表单或使用现有插件 - 简单地粘贴到旧的 html 表单中是行不通的,因为 Wordpress 将忽略任何它无法识别的已发布变量(即表单中的变量)。
  • @FluffyKitten 它重定向到 404 错误页面。我可能会放弃并使用一个插件——我认为修改我已经拥有的东西会更容易,但也许不是。我正在尝试做类似这个人所做的事情,但在一个页面内而不是在它自己的页面上。 premium.wpmudev.org/blog/… 而那个人使用 对于我自己尝试过的操作,但没有成功。
  • 同一页。使用时发生了什么?
  • @FluffyKitten 当我使用 ,它会显示一个 404 错误页面。该 URL 是主页(mywebsite.com)。我还尝试为主页制作一个单独的页面模板,而不是使用 front-page.php,因为另一个 Stack Overflow 线程建议这样做,但这也不起作用。我很困惑,我不知道为什么所有这些都重定向到 404 页面。我尝试过的每个解决方案都进入了 404 页面。

标签: javascript php jquery html wordpress


【解决方案1】:

我找到了自己问题的答案!我完全忘记了 GitHub 上的这段代码:https://github.com/dwyl/html-form-send-email-via-google-script-without-server 我在网站的其他地方使用它并且它有效,它也适用于页面内的表单!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-22
    • 2013-04-30
    相关资源
    最近更新 更多