MackMan
MackMan

Reputation: 129

PHP Contact form email specific people based on selection

Just wondering if there's a way to create variables in this PHP form I have been using. I am still a real newbie to this stuff, so be nice :)

I am hoping to create a variance log for multiple departments at the hospital I work at. I want to have a drop down that has all departments and whatever department they choose will determine who the email gets sent to (ie. managers email).

Here's what I have currently. (didn't attach style sheet, error, or thanks pages as I didn't think they were relevant)

var_contact.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <title>Variance Log Form</title>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

    <link rel="stylesheet" type="text/css" href="var_style.css" />
</head>

<body>

<div id="page-wrap">
    <div id="contact-area">
        <H1>Variance Log Form</H1>
        <br />
        <br />
        <form method="post" action="var_contactengine.php">
            <label for="Name">Name:</label>
            <input type="text" name="Name" id="Name" />

            <label for="Name">Dept:</label>
            <select name="Dept" id="Dept">
                  <option value="" selected class="list"></option>
                  <option value="Accounting">Accounting</option>
                  <option value="Administration">Administration</option>
                  <option value="Bariatric Services">Bariatric Services</option>
                  <option value="Bariatric Care">Bariatric Care</option>
                  <option value="Cardiac Rehab">Cardiac Rehab</option>
                  <option value="CBO">CBO</option>
                  <option value="Central Scheduling">Central Scheduling</option>
                  <option value="Dietary">Dietary</option>
                  <option value="ER">ER</option>
                  <option value="ICU">ICU</option>
                  <option value="IS">IS</option>
                  <option value="Infusion">Infusion</option>
                  <option value="Lab">Lab</option>
                  <option value="Materials">Materials</option>
                  <option value="Medical Records">Medical Records</option>
                  <option value="MedSurg">MedSurg</option>
                  <option value="OB">OB</option>
                  <option value="Pharmacy">Pharmacy</option>
                  <option value="Physical Therapy">Physical Therapy</option>
                  <option value="Pain Clinic">Pain Clinic</option>
                  <option value="BPIM">BPIM</option>
                  <option value="Georgetown Cardiology">Georgetown Cardiology</option>
                  <option value="Gastro">Gastro</option>
                  <option value="Oncology">Oncology</option>
                  <option value="Georgetown Bariatrics">Georgetown Bariatrics</option>
                  <option value="GFP">GFP</option>
                  <option value="General Surgery">General Surgery</option>
                  <option value="Neurology">Neurology</option>
                  <option value="Pulmonology">Pulmonology</option>
                  <option value="Urology">Urology</option>
                  <option value="Express Care">Express Care</option>
                  <option value="Plant-Ops">Plant-Ops</option>
                  <option value="Quality">Quality</option>
                  <option value="Radiology">Radiology</option>
                  <option value="Registration">Registration</option>
                  <option value="Respiratory Therapy">Respiratory Therapy</option>
                  <option value="Surgery">Surgery</option>

            </select>

            <label for="Email">Email:</label>
            <input type="text" name="Email" id="Email" />

            <label for="Message">Message:</label><br />
            <textarea name="Message" rows="20" cols="20" id="Message"></textarea>

            <input type="submit" name="submit" value="Submit" class="submit-button" />
        </form>

        <div style="clear: both;"></div>



    </div>

</div>

</body>

</html>

var_contactengine.php (This is the one I need help with. Not sure if I can use "IF" statements to say IF Dept=MedSurg [email protected])

<?php

$EmailFrom = "[email protected]";
$EmailTo = "[email protected]";
$Subject = "HR Contact Request";
$Name = Trim(stripslashes($_POST['Name'])); 
$Dept = Trim(stripslashes($_POST['Dept'])); 
$Message = Trim(stripslashes($_POST['Message'])); 

// validation
$validationOK=true;
if (!$validationOK) {
  print "<meta http-equiv=\"refresh\" content=\"0;URL=var_error.html\">";
  exit;
}

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= $"Dept:";
$Body .= $Dept;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";

// send email 
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

// redirect to success page 
if ($success){
  print "<meta http-equiv=\"refresh\" content=\"0;URL=var_contactthanks.php\">";
}
else{
  print "<meta http-equiv=\"refresh\" content=\"0;URL=var_error.htm\">";
}
?>

Upvotes: 0

Views: 174

Answers (1)

X A
X A

Reputation: 837

you can put everything in an array:

$emails = array(
    "Accounting" => "[email protected], [email protected]",
    "Radiology" => "[email protected], [email protected]",
);

then

if (!empty($Dept) && isset($emails[$Dept])) {
    $EmailTo = $emails[$Dept];
}

as a sidenote: I see you don't have any validation for $EmailFrom, you can use something like this

if (filter_var($EmailFrom, FILTER_VALIDATE_EMAIL)) {
    $validationOK=true;
}

Upvotes: 1

Related Questions