Solved

php email contact form with dropdown box for different email addresses

Posted on 2008-10-28
21
2,001 Views
Last Modified: 2013-12-13
Hi,

On the link http://hbni.net/contact.htm we have a basic php contact form that I would like to further develop. I would like to have a name field, email address, and the question and request. That is all there now. What I'd like to have is a drop down box in which a selection can be made to send emails to different departments.

Here are the fields I'd like in the dropdown box:

Technical Support
Webfilter Support
LifeSize Information Request
Member Password Request
Webmaster

Each of these would have an email sent to a different email address. I would prefer if someone could help me make this in php so it's easy to configure or modify. The form we currently use is from www.thesitewizard.com and was basic cut and paste. It would probably be easier to make a new one from scratch. Does anyone have something like this?

Thanks in Advance!
0
Comment
Question by:wingcat
  • 11
  • 5
  • 3
  • +2
21 Comments
 
LVL 27

Expert Comment

by:yodercm
ID: 22822022
Create a simple dropdown box with the choices, and in the action script, for each choice, send the email to whatever email address you wish.

The dropdown in the screen script:

<select name=dept>
<option>Technical Support</option>
<option>Webfilter Support</option>
etc.
</select>

Then in the action script:

$dept = $_POST["dept"];

if ($dept == "Technical Support")  $emailaddress="techsupport@myplace.com";
elseif ($dept == Wibfilter Support") $emailaddress = "webfilter@myplace.com";
etc...


0
 
LVL 15

Expert Comment

by:MMDeveloper
ID: 22822038
for security reasons.... I'd have the select menu setup like this

<select name="dept">
<option value="0">Technical Support</option>
<option value="1"> Webfilter Support</option>

--etc
</select>

The option value corresponds to an array of email addresses in your form processing script. That way spammers cant send email addresses to your script, and the dept's email addresses are not published in the HTML of the page.


In your PHP you'd have an array, to make it easier to visually reference, I'd go ahead and write out the index numbers even though it's not required.


<?php

$_POST["dept"] = abs(intval($_POST["dept"]));

$emailAddresses = array (

		0 => "techSupport@domain.com",

		1 => "webFilter@domain.com"

	);
 
 

$emailTo = ($emailAddresses[$_POST["dept"]] != "") ? $emailAddresses[$_POST["dept"]] : "defaultEmailAddress@domain.com" ;
 

//now $emailTo contains the email address.

?>

Open in new window

0
 
LVL 7

Expert Comment

by:aamodt
ID: 22822093
in the HTML form wirte

<select name=email>
<option value="The email of technical support">Technical Support</option>
<option value="The email of Webfilter Support">Webfilter Support</option>
<option value="The email of LifeSize Information Request">LifeSize Information Request</option>
<option value="The email of Member Password Request
">Member Password Request
</option>
<option value="The email of Webmaster">Webmaster</option>
</select>

----------------------------------------

on the php side, guess you know the code from thesitewizard?  just replace the $email variable to $_POST['email']

this will not garanty any securtiy but will solve your problem
--
your webmaster can proberlly help you with some security in the code. by trimming the input ect
0
 
LVL 27

Expert Comment

by:yodercm
ID: 22822271
Guys, by using my method

$dept = $_POST["dept"];

if ($dept == "Technical Support")  $emailaddress="techsupport@myplace.com";
elseif ($dept == Webfilter Support") $emailaddress = "webfilter@myplace.com";
etc...
else {// error message and exit}

there is no way that anyone can send any false data that will affect anything.  If the last else clause of the email checks is to send error message or exit, no user can get any other email addresses through, and there is no need for alternate values in the <option statements or any clever arrays of email addresses needed.
0
 
LVL 15

Expert Comment

by:MMDeveloper
ID: 22822368
it's just a lot of extra code and your conditions are case sensitive and assuming that the user is a good user. One should never rely on text comparisons, especially from a form input, too many ways it can go wrong even when it wasn't supposed to go wrong. For example, if a user's browser URL encoded the text

"Technical Support" != "Technical%20Support"

Also, if the department names change, they have to be updated and kept in perfect sync in two different files, else it wont work. Using a more scalable (design derived from database normalization techniques) solution is preferred. Not saying any of these options wont work.
0
 
LVL 27

Expert Comment

by:yodercm
ID: 22822469
If you're worried about URL encoding, then use a simple decode ....

$dept = urldecode($_POST["dept"]);

Using codes is poor programming practice and leads to bugs, errors, and at the very least, constant backchecking.
0
 
LVL 27

Expert Comment

by:yodercm
ID: 22822481
And by the way, case sensitive is GOOD.  If a user is attempting to change something for hacking purposes and doesn't match the case, you will catch it.

Remember, these are text strings produced by your own code, so the case sensitivity is exactly what you DO want.
0
 

Author Comment

by:wingcat
ID: 22822500
Can anyone make the entire php code for the form and the php script? I am receiving just parts of the code and I'm not sure how to put it all together. I am actually the 'webmaster' putting the site together, but I know nothing of php.
0
 
LVL 15

Expert Comment

by:MMDeveloper
ID: 22822529
if you want to write 10+ if/elseif statements, one for every department that's your cup of tea, I prefer a cleaner approach that involves less typing. EOC for me.
0
 
LVL 27

Expert Comment

by:yodercm
ID: 22822525
Actually, DON'T use urldecode, it's not needed, this is a POST method, not a URL, so it will NEVER be encoded.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 27

Expert Comment

by:yodercm
ID: 22822571
wingcat, I gave it to you in my first post:

Place this in your html where you want the dropdown to appear, within the <form...> and </form> that already appears for your other inputs.

<select name=dept>
<option>Technical Support</option>
<option>Webfilter Support</option>
etc.
</select>


Then at the beginning of the script named in the action part of the form statment, <form action='something.php'

add the following

$dept = $_POST["dept"];

if ($dept == "Technical Support")  $emailaddress="techsupport@myplace.com";
elseif ($dept == Wibfilter Support") $emailaddress = "webfilter@myplace.com";
etc...

expanding both parts for the different departments you need.  Then use the variable $emailaddress in the code where the email is sent.


0
 
LVL 27

Expert Comment

by:yodercm
ID: 22822618
@MMDeveloper, if this questioner were a php guru, I'd agree with you, but why make complex code that is difficult to understand, when a simple series of if statements will suffice?  

Someone who claims to "know nothing of php" can understand, code, and maintain simple if statements much more easily, and that is far more important than being clever about minimizing the number of keystrokes.
0
 

Author Comment

by:wingcat
ID: 22822720
Here is the code for the form. I am having trouble inserting the

<select name=dept>
<option>Technical Support</option>
<option>Webfilter Support</option>

into the code and making it work. could you help me with that?
<form action="feedback.php" method="post">

								<div align="center">

									<table border="0" cellpadding="8" cellspacing="8" summary="feedback form">

										<tr>

											<td><font size="2" class="bodys">Your Name:</font></td>

											<td><input type="text" name="name" size="25" /></td>

										</tr>

										<tr>

											<td><font size="2" class="bodys">Email address:</font></td>

											<td><input type="text" name="email" size="25" /></td>

										</tr>

										<tr>

											<td colspan="2"><p>&nbsp;</p>

											  <p><font size="2" class="bodys">Question or Request:</font><font size="1"><br />

										          </font>

											        <textarea rows="8" cols="67" name="comments"></textarea>

									          </p></td>

									  </tr>

										<tr>

											<td align="center" colspan="2"><input type="submit" value="Send " />

											<br />

											</td>

										</tr>

									</table>

								</div>

			</form>

Open in new window

0
 
LVL 27

Expert Comment

by:yodercm
ID: 22822721
In the script for the main webpage.....


<form method=post action='whatever.php'>

<input type=text name=name>
<input type=text name=email>
<input type=text name=request>
<input type=text name=question>

<select name=dept>
<option>Technical Support</option>
<option>Webfilter Support</option>
etc for each dept
</select>

<input type=submit>
</form>


In the script named whatever.php.....

$name = $_POST["name"];
$email = $_POST["email"];
$request = $_POST["request"];
$question = $_POST["question"];
$dept = $_POST["dept"];

if ($dept == "Technical Support")  $emailaddress="techsupport@myplace.com";
elseif ($dept == Webfilter Support") $emailaddress = "webfilter@myplace.com";
etc for each dept
else {exit;}

mail($email,$request,$question,"From: <webpagerequest@yourdomain.com>");



The email is sent by something similar to this, filling in your own From email and etc.
http://us2.php.net/manual/en/function.mail.php
0
 
LVL 27

Expert Comment

by:yodercm
ID: 22822735
Oops, I posted the above before I saw yours, but you should be able to put this into your own HTML easily enough I think.  Just change the names of things to match your own code.
0
 

Author Comment

by:wingcat
ID: 22822780
My form gets messed up when I inset it. Could you please give it a shot for me! Thanks,
0
 
LVL 27

Accepted Solution

by:
yodercm earned 350 total points
ID: 22822990
OK here's the shot, but it's not tested, so you can work it out from here :)
<form action="feedback.php" method="post">

<div align="center">

	<table border="0" cellpadding="8" cellspacing="8" summary="feedback form">

		<tr>

			<td><font size="2" class="bodys">Your Name:</font></td>

			<td><input type="text" name="name" size="25" /></td>

		</tr>

		<tr>

			<td><font size="2" class="bodys">Email address:</font></td>

			<td><input type="text" name="email" size="25" /></td>

		</tr>

		<tr>

			<td colspan="2"><p>&nbsp;</p>

				<p><font size="2" class="bodys">Question or Request:</font><font size="1"><br /></font>

				<textarea rows="8" cols="67" name="comments"></textarea>

				</p>

			</td>

		</tr>

		<tr>

			<td colspan="2">

				<select name="dept">

				<option>Technical Support</option>

				<option>Webfilter Support</option>

				<option>LifeSize Information Request</option>

				<option>Member Password Request</option>

				<option>Webmaster</option>

				</select>

			</td>

		</tr>

		<tr>

			<td align="center" colspan="2"><input type="submit" value="Send " /><br />

			</td>

		</tr>

	</table>

</div>

</form>
 
 
 

Then in feedback.php ...........
 
 

$name = $_POST["name"];

$email = $_POST["email"];

$question = $_POST["comments"];

$dept = $_POST["dept"];
 

if ($dept == "Technical Support")  $emailaddress="techsupport@yourdomain.com";

elseif ($dept == "Webfilter Support") $emailaddress = "webfilter@yourdomain.com";

elseif ($dept == "LifeSize Information Request") $emailaddress = "lifesize@yourdomain.com";

elseif ($dept == "Member Password Request") $emailaddress = "memberrqst@yourdomain.com";

elseif ($dept == "Webmaster") $emailaddress = "webmaster@yourdomain.com";

else {exit;}
 

mail($email,"Webpage Comments",$question,"From: <webpagerequest@yourdomain.com>");

Open in new window

0
 

Author Comment

by:wingcat
ID: 22823253
Wow it actually works! Thanks a million! One more thing, is it possible to redirect the sender to a thank you page? What code would you enter into the feedback.php file
0
 
LVL 27

Expert Comment

by:yodercm
ID: 22823320
Sorry, I don't know much about redirecting, so you probably need to ask it in a different thread.
0
 

Author Closing Comment

by:wingcat
ID: 31510740
You are truly an expert! Thanks a bunch.
0
 

Expert Comment

by:javahut
ID: 27592719
Question on this thread. I have the same situation, but instead of a single drop down... I have 2. I have this built right now with the if else statements for the request dropdown, but need to have the condition for wether it is residential or commercial for the property dropdown.

First dropdown selects a property and the second selects a request.

In the first drop down there is 5 addresses for residential and 2 for commercial. Residential goes to 1 set of notification email addresses and the commercial goes to a second set of email addresses. The requests stay the same in the second dropdown that determines where those emails go.

anyone have an idea on how to make this work?
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now