【问题标题】:thanks message instead of contact form感谢信息而不是联系表格
【发布时间】:2015-08-23 09:28:52
【问题描述】:

我制作了一个联系表单,我希望当用户发送邮件时,出现“您的消息已发送”消息而不是联系表单,我该如何解决?

这是我的代码

<form method="post" action="" name="contact">
    <div class="column">
        <input name="name" id="name" placeholder="name" value=""/>
    </div>
    <div class="column-2">
        <input name="email" id="email" placeholder="mail" value="" />
    </div>
    <div class="column-3">      
        <textarea id="message" placeholder="Your message" name="message" title="votre message" ></textarea>
    </div> 
    <div class="button">
        <span><input class="submit" id="submit" name="submit" type="submit" value="ENVOYER"></span>
    </div>
</form>

PHP 代码

<?php
if(!empty($_POST['name'])&&!empty($_POST['email'])&&!empty($_POST['message']))// check if everything has been filled out before doing anything else
{
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = 'From: test Contact';
    $to = 'test@test.com';
    $subject = 'Hello';

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

    if ($_POST['submit']) {
        if ($name != '' && $email != '') {

                if (mail ($to, $subject, $body, $from)) {
                    echo '<p>Your message has been sent!</p>
                    <span id="success">OK</span>';
                } else {
                    echo '<p>Something went wrong, go back and try again!</p>';
                }
            } 
        }
    }

?>

【问题讨论】:

    标签: php contact-form sendmessage


    【解决方案1】:

    您必须使用 AJAX。然后从你的 AJAX 调用你的 php 代码(你需要将你的 php 放在一个单独的文件夹中)

    【讨论】:

      【解决方案2】:

      如果你想实现它,那么你必须将成功或失败的页面重定向到另一个页面。我也稍微更改了您的代码结构,因为您需要先检查表单是否已提交。

      这是工作示例。

      index.php 页面

      <?php    
      if (isset($_POST['submit'])) {
          if (!empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['message'])) {// check if everything has been filled out before doing anything else
              $name = $_POST['name'];
              $email = $_POST['email'];
              $message = $_POST['message'];
              $from = 'From: test Contact';
              $to = 'test@test.com';
              $subject = 'Hello';
              $body = "From: $name\n E-Mail: $email\n Message:\n $message";
      
              if ($name != '' && $email != '') {
                  if (mail($to, $subject, $body, $from)) {
      
                      header("Location: result.php?success=1"); 
      
                  } else {
                       header("Location: result.php?success=0"); 
                  }
              } else {
                  echo "Name and Email not found";
              }
          } else {
              echo "Please fill up all fields";
          }
      }
      ?>
      
      
      <form method="post" action="" name="contact">
          <div class="column">
              <input name="name" id="name" placeholder="name" value=""/>
          </div>
          <div class="column-2">
              <input name="email" id="email" placeholder="mail" value="" />
          </div>
          <div class="column-3">      
              <textarea id="message" placeholder="Your message" name="message" title="votre message" ></textarea>
          </div> 
          <div class="button">
              <span><input type="submit" class="submit" id="submit" name="submit"  value="ENVOYER"></span>
          </div>
      </form>
      

      result.php 页面

      <?php
      if (isset($_GET['success']) && $_GET['success'] == 1) {
          echo '<p>Your message has been sent!</p><span id="success">OK</span>';
      } else {
          echo '<p>Something went wrong, go back and try again!</p>';
      }
      ?>
      

      【讨论】:

        【解决方案3】:

        你有三个选择。

        • 使用引导模式组件来显示消息。
        • 通过 AJAX 调用 PHP 函数。
        • 创建两个不同的页面(一个带有
          联系表格和另一个带有 PHP 代码的表格)。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-07-16
          • 1970-01-01
          • 2018-12-03
          • 2020-09-22
          • 2015-02-11
          相关资源
          最近更新 更多