Link to home
Start Free TrialLog in
Avatar of wingcat
wingcat

asked on

php email contact form with dropdown box for different email addresses

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!
Avatar of Cornelia Yoder
Cornelia Yoder
Flag of United States of America image

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...


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

Avatar of Thomas Aamodt
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
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.
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.
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.
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.
Avatar of wingcat
wingcat

ASKER

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.
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.
Actually, DON'T use urldecode, it's not needed, this is a POST method, not a URL, so it will NEVER be encoded.
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.


@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.
Avatar of wingcat

ASKER

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

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
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.
Avatar of wingcat

ASKER

My form gets messed up when I inset it. Could you please give it a shot for me! Thanks,
ASKER CERTIFIED SOLUTION
Avatar of Cornelia Yoder
Cornelia Yoder
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of wingcat

ASKER

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
Sorry, I don't know much about redirecting, so you probably need to ask it in a different thread.
Avatar of wingcat

ASKER

You are truly an expert! Thanks a bunch.
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?