0

EDITED:

I have a contact form using HTML/PHP. I would like users to be able to select a department from a dropdown menu, and have the email sent to just that department's email address.

My question is: Where in the PHP do I specify what email address the information is sent to based on the users dropdown selection. I'm assuming it's from this portion of the code:

HTML

            <label class="custom-select">
                <select name="department">
                    <option>Technical Issues</option>
                    <option>Charities</option>
                    <option>General Inquiries</option>
                    <option>Other</option>
                </select><span><!-- fake select handler --></span>
            </label>
            </div>
            <div class="column-100">

                <textarea name="message" placeholder="Type your message here..."></textarea>

            </div>
            <div class="column-100 center">
                <input type="submit" value="Send" data-error="Fix errors" data-processing="Sending..." data-success="Thank you!">
            </div>
            <footer class="notification-box"></footer>
        </form>

</div>

PHP

define('FROM_EMAIL', '');

// Recipient's e-mail. To this e-mail messages will be sent.
// e.g.: [email protected]
// multiple recipients e.g.: [email protected], [email protected]
define('TO_EMAIL', '[email protected]');

/**
 * Function for sending messages. Checks input fields, prepares message and sends it.
 */
function sendMessage() {
    // Variables init

Thanks in advance. :)

1
  • So what's your question? And before you say, "how do I do it?", remember that questions like that are too broad and will be closed as such. So if you've written any code thus far I highly recommend adding it to your question. You should read "how do I ask a good question?". Commented Aug 25, 2015 at 20:12

2 Answers 2

5

You're going to have to fix your HTML to pass a value so we know which is selected:

<select name="department">
   <option value="technical">Technical Issues</option>
   <option value="charities">Charities</option>
   <option value="general">General Inquiries</option>
   <option value="other">Other</option>
</select>

In your form processor pull out the value:

$dept = $_POST['department'];

Then switch statement to grab the proper email:

switch($dept) {
   case 'technical':
       $to = "[email protected]";
       break;

   //Rest of email cases
}

// Mail it!
$result = mail($to, $title, $message, $headers);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! That worked perfectly. I hadn't used switch statements before so thank you for explaining everything.
1

I'd set up some logic in the PHP to change the TO_EMAIL variable to the appropriate email. (Line 6 of your PHP)

switch ( $_POST['department'] ) {
    case "Technical Issues":
        define('TO_EMAIL', '[email protected]');
    break;
    case "Charities":
        define('TO_EMAIL', '[email protected]');
    break;
    case "General Inquiries":
        define('TO_EMAIL', '[email protected]');
    break;
    default:
    case "Other":
        define('TO_EMAIL', '[email protected]');
    break;
}

Wouldn't that work?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.