【问题标题】:PHP Contact form based on selection of department基于部门选择的 PHP 联系表
【发布时间】:2016-04-22 08:16:51
【问题描述】:

我在使用电子邮件表单时遇到了一些问题,希望你们能帮助我。问题只是当我提交消息时,消息没有发送给我。 (我已将 $to= 和标头位置更改为“假”地址)。但是,我在提交邮件时被重定向到正确的页面。

HTML:

  <section id="contact">
     <div class="container">
        <div class="row">
           <div class="col-lg-12">
              <form name="sentMessage"form action="contact.php" method="post" enctype="multipart/form-data id="contactForm" novalidate>
                 <div class="row">
                    <div class="col-md-6">
                       <div class="form-group">
                          <input type="text" class="form-control" placeholder="Your name *" id="name" required data-validation-required-message="Write your name">
                          <p class="help-block text-danger"></p>
                       </div>
                       <div class="form-group">
                          <input type="email" class="form-control" placeholder="Your email *" id="email" required data-validation-required-message="Write your email">
                          <p class="help-block text-danger"></p>
                       </div>
                       <div class="form-group">
                          <input type="tel" class="form-control" placeholder="Your phone number *" id="phone" required data-validation-required-message="Write your phone number">
                       </div>
                       <div class="form-group">
                          <select name="department" class="form-control">
                             <option value="support">Contact support</option>
                             <option value="department1">Contact department 1</option>
                             <option value="department2">Contact department 2</option>
                             <option value="department3">Contact department 3</option>
                          </select>
                       </div>
                      <!-- <div class="form-group">
                          <p>Last opp et vedlegg</p>
                          <input type="file" class="form-control" name="vedlegg" accept="image/*">
                       </div>
                    </div>-->
                    <div class="col-md-6">
                       <div class="form-group">
                          <textarea class="form-control" placeholder="Message *" id="message" required data-validation-required-message="Type a message"></textarea>
                       </div>
                    </div>
                    <div class="clearfix"></div>
                    <div class="col-lg-12 text-center">
                       <div id="success"></div>
                       <button type="submit" class="btn btn-xl">Send E-mail</button>
                    </div>
                 </div>
              </form>
           </div>
        </div>
     </div>
  </section>

这里是contact.php:

<?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $message = $_POST['message'];

    $from = 'From contact form'; 

    if( $_POST['department'] == "support" )
    {
        $subject = 'Mail to customer support';
         $to = "customer.support@mail.com";
          header("Location: www.domain.com/supportSuccess.html");
    }
   else if( $_POST['department'] == "department1" )
    {
         $subject = 'Mail to department1';
         $to = "department1@mail.com";
          header("Location: www.domain.com/departmentSucess.html");

    }
       else if( $_POST['department'] == "department2" )
    {
         $subject = 'Mail to department2';
         $to = "department2@mail.com";
          header("Location: www.domain.com/departmentSucess.html");

    }
           else if( $_POST['department'] == "department3" )
    {
         $subject = 'Mail to department3';
         $to = "department3@mail.com";
          header("Location: www.domain.com/departmentSucess.html");

    }

    $body = "$name\n $email\n $phone\n $message";
?>

<?php
if ($_POST['submit']) {
    if (mail ($to, $subject, $body, $from)) { 
        header("Location:www.google.com");
    } else { 
        echo '<p>Seems like something went wrong!</p>'; 
    }
}
?>

如果有人能提供一些指点,我将不胜感激。

谢谢!

【问题讨论】:

  • 由于if-else 之前的标头,您的代码控制不会进入mail 函数。设置变量并存储页面名称,然后在邮件功能后设置标题。
  • 您是否使用js脚本提交表单,如果没有,则需要将我们的输入元素id="something"更改为name="something"
  • @guru 您只是缺少修改 1 行代码,请在下面查看我的答案

标签: php html forms contact


【解决方案1】:

您忘记检查是否设置了 $_POST['submit'] 变量

改变

if ($_POST['submit']) {//...

if (isset($_POST['submit'])) {//...

【讨论】:

  • 解决了所有问题。非常感谢!
  • @gur 没问题老兄:)
【解决方案2】:
  • 一开始,您的 HTML 代码中的 enctype 部分缺少一个 "。
  • 您应该检查用户是否单击了提交按钮,然后开始验证您的数据。我做了一个快速修复。

    // First you check if the button submit is been clicked
    if ( isset($_POST['submit']) ) {
    
      $name = $_POST['name'];
      $email = $_POST['email'];
      $phone = $_POST['phone'];
      $message = $_POST['message'];
    
      $from = 'From contact form'; 
    
    
    
      if( $_POST['department'] == "support" )
      {
          $subject = 'Mail to customer support';
           $to = "customer.support@mail.com";
            header("Location: www.domain.com/supportSuccess.html");
      }
    
    
      // You could better wrap this into a switch case statement. 
    
     else if( $_POST['department'] == "department1" )
      {
           $subject = 'Mail to department1';
           $to = "department1@mail.com";
            header("Location: www.domain.com/departmentSucess.html");
    
      }
      else if( $_POST['department'] == "department2" )
      {
           $subject = 'Mail to department2';
           $to = "department2@mail.com";
            header("Location: www.domain.com/departmentSucess.html");
    
      }
      else if( $_POST['department'] == "department3" )
      {
           $subject = 'Mail to department3';
           $to = "department3@mail.com";
            header("Location: www.domain.com/departmentSucess.html");
    
      }
    
      $body = "$name\n $email\n $phone\n $message";
    
        if (mail ($to, $subject, $body, $from)) { 
            header("Location:www.google.com");
        } else { 
            echo '<p>Seems like something went wrong!</p>'; 
        }
    
    
    }
    

现在你是 HTML

<section id="contact">
         <div class="container">
            <div class="row">
               <div class="col-lg-12">
                  <form name="sentMessage"form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data" id="contactForm" novalidate>
                     <div class="row">
                        <div class="col-md-6">
                           <div class="form-group">
                              <input type="text" class="form-control" placeholder="Your name *" id="name" required data-validation-required-message="Write your name">
                              <p class="help-block text-danger"></p>
                           </div>
                           <div class="form-group">
                              <input type="email" class="form-control" placeholder="Your email *" id="email" required data-validation-required-message="Write your email">
                              <p class="help-block text-danger"></p>
                           </div>
                           <div class="form-group">
                              <input type="tel" class="form-control" placeholder="Your phone number *" id="phone" required data-validation-required-message="Write your phone number">
                           </div>
                           <div class="form-group">
                              <select name="department" class="form-control">
                                 <option value="support">Contact support</option>
                                 <option value="department1">Contact department 1</option>
                                 <option value="department2">Contact department 2</option>
                                 <option value="department3">Contact department 3</option>
                              </select>
                           </div>
                          <!-- <div class="form-group">
                              <p>Last opp et vedlegg</p>
                              <input type="file" class="form-control" name="vedlegg" accept="image/*">
                           </div>
                        </div>-->
                        <div class="col-md-6">
                           <div class="form-group">
                              <textarea class="form-control" placeholder="Message *" id="message" required data-validation-required-message="Type a message"></textarea>
                           </div>
                        </div>
                        <div class="clearfix"></div>
                        <div class="col-lg-12 text-center">
                           <div id="success"></div>
                           <button type="submit" name="submit" class="btn btn-xl">Send E-mail</button>
                        </div>
                     </div>
                  </form>
               </div>
            </div>
         </div>
      </section>

【讨论】:

    猜你喜欢
    • 2011-02-05
    • 1970-01-01
    • 2021-05-27
    • 1970-01-01
    • 2016-02-18
    • 2014-02-22
    • 2012-10-04
    • 2015-06-23
    • 1970-01-01
    相关资源
    最近更新 更多