[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Problems with php form - want to use a dropdown to choose the department an email goes to

Posted on 2009-05-20
5
Medium Priority
?
248 Views
Last Modified: 2012-05-07
Hi
I've taken some code from another question on this site but it won't work for me. I am trying to create a contact form that sends an email to a specific department based on the dropdown option selected.

When I load the files onto my server and submit, all that happens is I get directed to the php page but no email comes through.

Please could you have a look at my code and see if I'm missing something or doing something wrong. Thanks in advance!
Heres the 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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link type="text/css" rel="stylesheet" href="my.css">
 
<title>Untitled Document</title>
</head>
 
<body>
<form action="feedback.php" method="post">
<div align="center">
	<table border="0" cellpadding="8" cellspacing="8" summary="feedback form">
		<tr>
			<td><div align="left"><font size="2" class="bodys">Your Name:</font></div></td>
			<td><div align="left">
			  <input type="text" name="name" size="25" />
			  </div></td>
		</tr>
		<tr>
			<td><div align="left"><font size="2" class="bodys">Email address:</font></div></td>
			<td><div align="left">
			  <input type="text" name="email" size="25" />
			  </div></td>
		</tr>
		<tr>
			<td colspan="2"><p align="left"><font size="2" class="bodys">Question or Request:</font><font size="1"><br /></font>
				<textarea rows="8" cols="40" name="comments"></textarea>
			</p>			</td>
		</tr>
		<tr>
			<td colspan="2">
				<div align="left">
				  <select name="dept">
				    <option>Logistics</option>
				    <option>HPI Queries</option>
				    <option>V5 Queries</option>
			      </select>
                </div></td>
		</tr>
		<tr>
			<td align="center" colspan="2"><div align="left">
			  <input type="submit" value="Send " />
			  <br />			
		    </div></td>
		</tr>
	</table>
</div>
</form>
 
</body>
</html>
 
and here's the php:
 
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link type="text/css" rel="stylesheet" href="my.css">
<title>Untitled Document</title>
</head>
 
<body>
<?php
$name = $_POST["name"];
$email = $_POST["email"];
$question = $_POST["comments"];
$dept = $_POST["dept"];
 
if ($dept == "Logistics")  $emailaddress="myemail1@gmail.com";
elseif ($dept == "HPI Queries") $emailaddress = "myemail2@gmail.com";
elseif ($dept == "V5 Queries") $emailaddress = "myemail3@googlemail.com";
 
else {exit;}
 
mail($email,"Webpage Comments",$question,"From: <myemail1@gmail.com>");
 
?>
</body>
</html>

Open in new window

0
Comment
Question by:GizmoPig
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 17

Accepted Solution

by:
Chris Harte earned 2000 total points
ID: 24430758
You have $email and $emailaddress which is never used. Submit the address as a value when you select the drop down. See attached code

Heres the 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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link type="text/css" rel="stylesheet" href="my.css">
 
<title>Untitled Document</title>
</head>
 
<body>
<form action="feedback.php" method="post">
<div align="center">
<table border="0" cellpadding="8" cellspacing="8"
	summary="feedback form">
	<tr>
		<td>
		<div align="left"><font size="2" class="bodys">Your Name:</font></div>
		</td>
		<td>
		<div align="left"><input type="text" name="name" size="25" /></div>
		</td>
	</tr>
	<tr>
		<td>
		<div align="left"><font size="2" class="bodys">Email address:</font></div>
		</td>
		<td>
		<div align="left"><input type="text" name="email" size="25" /></div>
		</td>
	</tr>
	<tr>
		<td colspan="2">
		<p align="left"><font size="2" class="bodys">Question or Request:</font><font
			size="1"><br />
		</font> <textarea rows="8" cols="40" name="comments"></textarea></p>
		</td>
	</tr>
	<tr>
		<td colspan="2">
		<div align="left"><select name="dept">
			<option value="logistics@gmail.com">Logistics</option>
			<option value="hpt_queries@gmail.com">HPI Queries</option>
			<option value="v5_queries@gmail.com">V5 Queries</option>
		</select></div>
		</td>
	</tr>
	<tr>
		<td align="center" colspan="2">
		<div align="left"><input type="submit" value="Send " /> <br />
		</div>
		</td>
	</tr>
</table>
</div>
</form>
 
</body>
</html>
 
and here's the php:
 
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link type="text/css" rel="stylesheet" href="my.css">
 
 
<title>Untitled Document</title>
</head>
 
<body>
<?php
$name = $_POST["name"];
$email = $_POST["email"];
$question = $_POST["comments"];
$dept = $_POST["dept"];
 
 
mail($dept, "Webpage Comments", $question, "From: $email");
 
?>
</body>
</html>

Open in new window

0
 

Author Closing Comment

by:GizmoPig
ID: 31583431
That worked beautifully. Thanks so much for your help - I was tearing my hair out over this one!!
0
 

Author Comment

by:GizmoPig
ID: 24431286
Just come up against one slight problem - I need to display the name and email fields in the emails that are sent as well. Adding the name and email tags like so -  mail($dept, "Webpage Comments", $name,$email,$question, "From: $email"); seems to break the code. Any suggestions at all? Thanks
0
 
LVL 17

Expert Comment

by:Chris Harte
ID: 24431369
This question is closed so you should open a new one, but::: you have changed the number of arguments, don't. put it in the header at the end
mail($dept, "Webpage Comments", $question, "From: $email name $name");
0
 

Author Comment

by:GizmoPig
ID: 24431453
Sorry - I wasn't very clear there. I need the name and email of the person who sent the request to appear in the body of the email. This solution makes it appear in the address from line.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

650 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