PHP Contact Form with selectable recepient

Good Morning

I have 'googled' in vain to find a suitable tutorial to help create a php contact form with a user selectable recepient. Basicaly I would like to use the same simple (Name, Email, Enquiry) contact form, but send it to one of three email addresses based on a selection made by the user completing the form. For example if Sales is select then it will be emailed to sales@, Servicing will be emailed to servicing@ or Training emailed to training@

My php knowledage is limited - so is my coding but I can normal follow a good tutorial.

Thanks
WashcareAsked:
Who is Participating?
 
Julian HansenConnect With a Mentor Commented:
Very simple sample
HTML
<form action="send.php" method="post">
 <label for="name">Name</label><input type="text" id="name" name="fullname" /><br>
 <label for="email">Email</label><input type="text" id="email" name="email" /><br>
 <label for="sendto">Send To</label><select  id="sendto" name="sendto">
<option>Please select one</option>
<option value="sales">Sales</option>
<option value="training">Training</option>
<option value="service">Service</option>
</select>
<input type="submit" >
</form>

Open in new window

PHP Code
<?php
// CHECK FOR VALID INPUT HERE
$fullname = isset($_POST['fullname']) ? $_POST['fullname'] : false;
$email = isset($_POST['email']) ? $_POST['email'] : false;
$sendto = isset($_POST['sendto']) ? $_POST['sendto'] : false;

// SETUP YOUR MAIL ADDRESSES HERE
// KEY IS THE VALUE THAT IS SENT FROM THE FORM
// VALUE IS THE EMAIL
$address = array(
   'sales' => 'sales@somewhere.com',
   'training' => 'training@somewhere.com',
   'service' => 'service@somewhere.com'
);

// POTENTIALLY MORE SANITISING / VALIDATION REQUIRED 
// CHECK WE HAVE EVERYTHING WE NEED
if ($fullname && $email && $sendto && isset($address[$sendto])) {
   // at this point we would probably use a library like PHPMailer to send the email
   // this demo uses PHP send - which is not advised - but without know more about
   // the use case difficult to advise on how to send email.

   mail($address[$sendto], "Subject", "Message goes here");
}

Open in new window


More on PHPMailer here
3
 
WashcareAuthor Commented:
Sorry for my slow reply - super thank you!
0
 
WashcareAuthor Commented:
Thanks
0
 
Julian HansenCommented:
You are welcome.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.