guru
guru

Reputation: 66

PHP Contact form based on selection of department

i am having some problems with getting a e-mail form to work and hope you guys can help me. The problem is simply when i submit a message the message is not sent to me. (i have changed the $to=, and header location to "fake" addresses). However, i get redirected to the right page when mail is submitted.

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>

Here is the 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 = "[email protected]";
          header("Location: www.domain.com/supportSuccess.html");
    }
   else if( $_POST['department'] == "department1" )
    {
         $subject = 'Mail to department1';
         $to = "[email protected]";
          header("Location: www.domain.com/departmentSucess.html");

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

    }
           else if( $_POST['department'] == "department3" )
    {
         $subject = 'Mail to department3';
         $to = "[email protected]";
          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>'; 
    }
}
?>

I would be grateful if anyone could provide some pointers.

Thanks!

Upvotes: 0

Views: 474

Answers (2)

Webeng
Webeng

Reputation: 7123

You forgot to check if the $_POST['submit'] variable was set

Change

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

to

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

Upvotes: 1

Duikboot
Duikboot

Reputation: 1101

  • As start, you are missing an " in your HTML code at the part enctype.
  • You should check if a user clicks the submit button and then start to validate your data. I made a quick fix.

    // 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 = "[email protected]";
            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 = "[email protected]";
            header("Location: www.domain.com/departmentSucess.html");
    
      }
      else if( $_POST['department'] == "department2" )
      {
           $subject = 'Mail to department2';
           $to = "[email protected]";
            header("Location: www.domain.com/departmentSucess.html");
    
      }
      else if( $_POST['department'] == "department3" )
      {
           $subject = 'Mail to department3';
           $to = "[email protected]";
            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>'; 
        }
    
    
    }
    

Now you 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>

Upvotes: 0

Related Questions