Solution 1 :

Passed param to mail function.

$name       = $_POST['name'];
$email      = $_POST['email'];
$phone      = $_POST['phone'];
$dept       = $_POST['department'];
$comments   = $_POST['comments'];

if(trim($name) == '') {
    echo '<div class="error_message" style="color: #de493e; font-weight: 700;">Please enter your name. Don't be shy!</div>';
    exit();
} else if(trim($email) == '') {
    echo '<div class="error_message" style="color: #de493e; font-weight: 700;">Please enter your email address, so we can get back to you!</div>';
    exit();
} else if(!isEmail($email)) {
    echo '<div class="error_message" style="color: #de493e; font-weight: 700;">Invalid e-mail address, try again!</div>';
    exit();
} else if(trim($comments) == '') {
    echo '<div class="error_message" style="color: #de493e; font-weight: 700;">You forgot to enter your message!</div>';
    exit();
} 

if($dept == "audio") {
    $address = "[email protected]"; 
} else if ($dept == "graphics") {
    $address = "[email protected]";
} else if ($dept == "music") {
    $address = "[email protected]";
} else if ($dept == "photography") {
    $address = "[email protected]";
} else if ($dept == "videography") {
    $address = "[email protected]";
} else if ($dept == "development") {
    $address = "[email protected]";
} else if ($dept == "other") {
    $address = "[email protected]";
}

$message = "First line of textnSecond line of text";
$received_subject = "My subject";
if(mail($address, $received_subject, $message)) {

    // Email has sent successfully, echo a success page.

    echo "<h2>Email Sent Successfully.</h2>";
    echo "<p>Thank you <strong>$name</strong>, your message has been sent to us.</p>";
    echo "<p>We will get back to you within 24 hours!</p>";

} else {

    echo '<h2>ERROR!</h2>';

}

Problem :

I have a contact form with the following fields:

  1. Name
  2. Email
  3. Number
  4. Department
  5. Message

“Department” is a drop down selection with the seven following options

  1. Audio Engineering
  2. Graphic Design
  3. Music Production
  4. Photography
  5. Videography
  6. Web Development
  7. Other

I have been trying to set it up in such a way where the email address that the data is being sent to, changes based on the user’s drop-down selection. For example, if the user selects Option 1, “Audio Engineering“, the email will be sent to “[email protected]“. If the user selects Option 2, “Graphic Design“, the email will be sent to “[email protected]” and so on, upon submission of the form. However, my current code returns an error.

This is my HTML.

<form method="post" action="contact.php" name="contactform" id="contactform">

      <fieldset id="contact_form">
          <label for="name">
              <input type="text" name="name" id="name" placeholder="ENTER YOUR NAME">
          </label>

          <label for="email">
              <input type="email" name="email" id="email" placeholder="ENTER YOUR EMAIL">
          </label>

          <label for="phone">
              <input type="text" name="phone" id="phone" placeholder="PHONE NUMBER">
          </label>

          <label for="department">
              <select name="department" id="department" class="cs-select cs-skin-border" required>
                  <option value="" disabled selected>DEPARTMENT</option>
                  <option value="audio">Audio Engineering</option>
                  <option value="graphics">Graphic Design</option>
                  <option value="music">Music Production</option>
                  <option value="photography">Photography</option>
                  <option value="videography">Videography</option>
                  <option value="development">Web Development</option>
                  <option value="other">Other</option>
              </select>
          </label>

          <label for="comments">
              <textarea name="comments" id="comments" placeholder="ENTER YOUR MESSAGE"></textarea>
          </label>

          <input type="submit" class="submit btn btn-default btn-black" id="submit" value="Submit">
      </fieldset>

</form>

This is the snippet of my PHP where I use if statements to change emails based on selection.

<?php

$name       = $_POST['name'];
$email      = $_POST['email'];
$phone      = $_POST['phone'];
$dept       = $_POST['department'];
$comments   = $_POST['comments'];

if(trim($name) == '') {
    echo '<div class="error_message" style="color: #de493e; font-weight: 700;">Please enter your name. Don't be shy!</div>';
    exit();
} else if(trim($email) == '') {
    echo '<div class="error_message" style="color: #de493e; font-weight: 700;">Please enter your email address, so we can get back to you!</div>';
    exit();
} else if(!isEmail($email)) {
    echo '<div class="error_message" style="color: #de493e; font-weight: 700;">Invalid e-mail address, try again!</div>';
    exit();
} else if(trim($comments) == '') {
    echo '<div class="error_message" style="color: #de493e; font-weight: 700;">You forgot to enter your message!</div>';
    exit();
} 

if($dept == "audio") {
    $address = "[email protected]"; 
} else if ($dept == "graphics") {
    $address = "[email protected]";
} else if ($dept == "music") {
    $address = "[email protected]";
} else if ($dept == "photography") {
    $address = "[email protected]";
} else if ($dept == "videography") {
    $address = "[email protected]";
} else if ($dept == "development") {
    $address = "[email protected]";
} else if ($dept == "other") {
    $address = "[email protected]";
}

if(mail($address, $received_subject, $message, $header)) {

    // Email has sent successfully, echo a success page.

    echo "<h2>Email Sent Successfully.</h2>";
    echo "<p>Thank you <strong>$name</strong>, your message has been sent to us.</p>";
    echo "<p>We will get back to you within 24 hours!</p>";

} else {

    echo '<h2>ERROR!</h2>';

}

?>

This unfortunately returns “ERROR!” and i’m not sure why.

I would also like to return an error message if no selection is made.

Comments

Comment posted by Manu Varghese

echo

Comment posted by dhamo

$success = mail($address, $received_subject, $message, $header); if (!$success) { $errorMessage = error_get_last()[‘message’]; print($errorMessage); }

Comment posted by vadivel a

can you pass all parameter to mail function

Comment posted by Manu Varghese

$received_subject, $header, $message,

Comment posted by David

I’m not sure I understand how this fixes the issue where the email doesn’t change based on the drop down selected and returns an Error instead.

Comment posted by vadivel a

You want validation for without select dropdown?

Comment posted by David

Yes. Similar to how I did validation for the other fields, using an if statement. So if nothing is selected, it’ll echo an error. But the main issue is I’m not getting the emails to switch based on the selected option..

By