/    Sign up×
Community /Pin to ProfileBookmark

PHP Contact form question

I’m setting up this basic PHP form:
[url]http://incentivo.dreamhosters.com/contact/[/url]

When someone selects one of the “My Question is for”

Whatever department they select, can I have the form emailed to that specific email address?

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@ayveghOct 24.2008 — My automatic response is to include the target email addresses in the HTML using the value tag, but that lends itself to abuse, since someone with little knowledge can just change it, so I came up with this instead:

Put this somewhere, preferably near the top, but just anywhere before the form and before the email sending code will work fine:
[code=php]$departments = array();

// $departments[] = array('Department Name', '[email protected]');
$departments[] = array('Finance Department', '[email protected]');
$departments[] = array('Main Office', '[email protected]');
$departments[] = array('Billing', '[email protected]');
$departments[] = array('Other', '[email protected]');[/code]

This goes in your contact form (replacing your current code):
[code=php]<select name="questiondropdown" size="1" class="contactRightPanel">
<option value="-1">-- Select One --&nbsp;&nbsp;</option>
<?php
foreach ($departments as $id => $info)
{
print('<option value="' . $id . '">' . $info[0] . '</option>' . "n");
}
?>
</select>[/code]

And then this goes into your email sending code:
[code=php]if (is_numeric($_REQUEST['questiondropdown']) AND $_REQUEST['questiondropdown'] != '-1' AND array_key_exists($_REQUEST['questiondropdown'], $departments))
{
$email_address_to_send_to = $departments[$_REQUEST['questiondropdown']][1];
}
else
{
// no department selected, or no departments specified in array, or hacking attempt
// error handling goes here
}
[/code]
Copy linkTweet thisAlerts:
@bdavey311authorOct 27.2008 — Thanks for the quick response. I just got back into town and saw what you had put together. You would think I would know where my email sending code is but I'm not sure. Got this PHP from someone else.

Here is what I have for PHP:
[code=php]<?php
header("Location: thankyou.html");
if(isset($_POST['submit'])) {

$departments = array();

// $departments[] = array('Department Name', '[email protected]');
$departments[] = array('Finance Department', '[email protected]');
$departments[] = array('Main Office', '[email protected]');
$departments[] = array('Billing', '[email protected]');
$departments[] = array('Other', '[email protected]');

$to = "[email protected]";
$subject = "LoHi Scooters Contact Form";
$first_name = $_POST['firstname'];
$last_name = $_POST['lastname'];
$contact_email = $_POST['emailaddress'];
$phone_1 = $_POST['phone1'];
$address_1 = $_POST['address1'];
$address_2 = $_POST['address2'];
$contact_city = $_POST['city'];
$contact_state = $_POST['state'];
$contact_zipcode = $_POST['zipcode'];
$contact_question = $_POST['questiondropdown'];
$contact_subject = $_POST['subject'];
$contact_message = $_POST['message'];
$preferred_checkbox = $_POST['check[]'];

foreach($_POST['check'] as $value) {
$check_msg .= "Preferred Method of Contact: $valuen";
}

$body = "First Name: $first_namenLast Name: $last_namenE-Mail: $contact_emailnnPhone: $phone_1nAddress 1: $address_1nAddress 2: $address_2nCity: $contact_citynState: $contact_statenZip Code: $contact_zipcodennMy Question is Regarding: $contact_questionnSubject: $contact_subjectn$check_msg nMessage: $contact_messagen";

echo "Data has been submitted to $to!";
mail($to, $subject, $body);

} else {
echo "Please use the form to email us.";
}
?>[/code]


Where do I put:
[code=php]if (is_numeric($_REQUEST['questiondropdown']) AND $_REQUEST['questiondropdown'] != '-1' AND array_key_exists($_REQUEST['questiondropdown'], $departments))
{
$email_address_to_send_to = $departments[$_REQUEST['questiondropdown']][1];
}
else
{
// no department selected, or no departments specified in array, or hacking attempt
// error handling goes here
} [/code]



Sorry for the noobness.

Thanks in advance.
Copy linkTweet thisAlerts:
@ayveghOct 28.2008 — This is where the actual sending takes place:
[code=php]mail($to, $subject, $body);[/code]
To make it send to where you want according to the selection, you need to insert that code after the default "To: ($to)", like this (actually, it will work anywhere after that, but it helps to keep the code together ?).

Here's your finished code:
[code=php]<?php
header("Location: thankyou.html");
if(isset($_POST['submit']))
{
$departments = array();

// $departments[] = array('Department Name', '[email protected]');
$departments[] = array('Finance Department', '[email protected]');
$departments[] = array('Main Office', '[email protected]');
$departments[] = array('Billing', '[email protected]');
$departments[] = array('Other', '[email protected]');

$to = "[email protected]";

if (is_numeric($_REQUEST['questiondropdown']) AND $_REQUEST['questiondropdown'] != '-1' AND array_key_exists($_REQUEST['questiondropdown'], $departments))
{
$to = $departments[$_REQUEST['questiondropdown']][1];
}

$subject = "LoHi Scooters Contact Form";
$first_name = $_POST['firstname'];
$last_name = $_POST['lastname'];
$contact_email = $_POST['emailaddress'];
$phone_1 = $_POST['phone1'];
$address_1 = $_POST['address1'];
$address_2 = $_POST['address2'];
$contact_city = $_POST['city'];
$contact_state = $_POST['state'];
$contact_zipcode = $_POST['zipcode'];
$contact_question = $_POST['questiondropdown'];
$contact_subject = $_POST['subject'];
$contact_message = $_POST['message'];
$preferred_checkbox = $_POST['check[]'];

foreach($_POST['check'] as $value)
{
$check_msg .= "Preferred Method of Contact: $valuen";
}

$body = "First Name: $first_namenLast Name: $last_namenE-Mail: $contact_emailnnPhone: $phone_1nAddress 1: $address_1nAddress 2: $address_2nCity: $contact_citynState: $contact_statenZip Code: $contact_zipcodennMy Question is Regarding: $contact_questionnSubject: $contact_subjectn$check_msg nMessage: $contact_messagen";

echo "Data has been submitted to $to!";
mail($to, $subject, $body);
}
else
{
echo "Please use the form to email us.";
}
?>[/code]

[B]Note:[/B] I'm not sure why this [code=php]header("Location: thankyou.html");[/code] is in there, but if you leave it in there, the script will obviously not work. ?

Enjoy,

ayvegh
Copy linkTweet thisAlerts:
@bdavey311authorOct 28.2008 — Thanks for the reply. I'm almost there.

You can view it here:

http://incentivo.dreamhosters.com/contact/

The form is sent to my address and works correctly, but the "My Question for" only has 1 option which says ". $info[0] ."

And that one line you said you weren't sure why it was in there in the confirmation page. The thank you.

Thanks in advance.
Copy linkTweet thisAlerts:
@ayveghOct 29.2008 — Post the part of your HTML where you inserted this (or your entire source code would work best):
This goes in your contact form (replacing your current code):
[code=php]<select name="questiondropdown" size="1" class="contactRightPanel">
<option value="-1">-- Select One --&nbsp;&nbsp;</option>
<?php
foreach ($departments as $id => $info)
{
print('<option value="' . $id . '">' . $info[0] . '</option>' . "n");
}
?>
</select>[/code]
[/QUOTE]
Copy linkTweet thisAlerts:
@bdavey311authorOct 29.2008 — Here is the HTML file:

[code=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>Associa&ccedil;&atilde;o Brasileira Incentivo - Contato</title>
<link media="screen" type="text/css" rel="stylesheet" href="/resources/css/homepage.css"/>
<link media="screen" type="text/css" rel="stylesheet" href="/resources/css/diagram.css"/>
<link media="screen" type="text/css" rel="stylesheet" href="/resources/css/view.css"/>
<script type="text/javascript" src="/resources/scripts/hover.js"></script>
</head>

<body id="quem-somos">
<div id="banner">
<div id="nav-global-container">
<ul id="nav-global">
<li id="nav-contact"><a href="/contact/">Contato</a></li><li>|</li>
<!-- <li id="nav-history"><a href="/historia/">Histori&aacute;</a></li><li>|</li> -->
<li id="nav-background"><a href="/quem-somos/">Quem somos</a></li><li>|</li>
<li id="nav-home"><a href="/index.html">Home</a></li>
</ul>
</div>
</div>

<div id="container">

<div id="header">

<h1 id="logo"><a href="/index.html"><img alt="Incentivo" src="/resources/images/logo.png"/></a></h1>

<div id="nav-sub-container">
<ul id="nav-sub">
<li id="first"><div>Programas</div>
<ul>
<li><a href="/bolsa-de-estudo/">Bolsa de Estudo</a></li>
<li><a href="/financas-pessoais/">Finan&ccedil;as Pessoais</a></li>
<li><a href="/preparacao-para-mba/">Prepara&ccedil;&atilde;o para MBA</a></li>
</ul>
</li>
<li><div>Filosofia</div>
<ul>
<li><a href="/passar-adiante/">Passar adiante</a></li>
<li><a href="/successo/">Hist&oacute;ria de successo</a></li>
<li><a href="/voluntarios/">Volunt&aacute;rios</a></li>
</ul>
</li>
<!--
<li id="last"><div>Forums</div>
<ul>
<li><a href="/forums/">Bolsa</a></li>
<li><a href="/forums/">Financas</a></li>
<li><a href="/forums/">MBA</a></li>
</ul>
</li>
-->
</ul>
</div>
</div>

<div id="billboard"><h2>Incentivo</h2></div>

<div id="columnwrap">

<div id="content-main">

<h1>

Contato</h1>
<p><span class="contactLeftPanel">Your comments and concerns are important to us. Please provide the following information so that we can address your needs. We will email a response to you within three business days.</span></p>
<p><span class="contactLeftPanel"><span class="contactAsteriskIndicator">* Indicates required information</span></span></p>

<form method="post" action="/resources/scripts/mailer.php">


<table width="465" height="572" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="465" height="50" align="left" valign="top" class="darkGreyLeftPanel">My Question is for * <br />
<select name="questiondropdown" size="1" class="contactRightPanel">
<option value="-1">-- Select One --</option>
<?php
foreach ($departments as $id => $info)
{
print('<option value="' . $id . '">' . $info[0] . '</option>' . "n");
}
?>
</select>
</td>
</tr>
<tr>
<td width="465" height="45" align="left" valign="top" class="darkGreyLeftPanel">Subject * <br />
<input name="subject" type="text" class="contactRightPanel" size="25" />
</td>
</tr>
<tr>
<td width="465" height="79" align="left" valign="top" class="darkGreyLeftPanel">Message | Comments *<br />
<textarea name="message" cols="75" rows="3" class="contactRightPanel"></textarea></td>
</tr>
<tr>
<td width="465" height="40" align="left" valign="top" class="darkGreyLeftPanel">What is your prefered method of contact? <br />
<table width="465" height="25" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="40" height="25" align="left" valign="middle" class="darkGreyLeftPanel">Email</td>
<td width="40" height="25" align="left" valign="middle" class="darkGreyLeftPanel"><input type="checkbox" name="check[]" value="Email"></td>
<td width="40" height="25" align="left" valign="middle" class="darkGreyLeftPanel">Phone</td>
<td width="40" height="25" align="left" valign="middle" class="darkGreyLeftPanel"><input type="checkbox" name="check[]" value="Phone"></td>
<td width="125" height="25" align="left" valign="middle" class="darkGreyLeftPanel">No Response Required </td>
<td width="195" height="25" align="left" valign="middle" class="darkGreyLeftPanel"><input type="checkbox" name="check[]" value="No Response Required"></td>
</tr>
</table></td>
</tr>
<tr>
<td width="465" height="50" align="left" valign="top" background="images/contact/yourinformation.gif">&nbsp;</td>
</tr>
<tr>
<td width="465" height="40" align="left" valign="top"><table width="465" height="40" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="182" height="40" align="left" valign="top" class="darkGreyLeftPanel">First Name *
<input name="firstname" type="text" class="contactRightPanel" size="25" />
</td>
<td width="293" align="left" valign="top"><span class="darkGreyLeftPanel">Last Name * <br />
<input name="lastname" type="text" class="contactRightPanel" size="25" />
</span></td>
</tr>
</table></td>
</tr>
<tr>
<td width="465" height="45" align="left" valign="middle" class="darkGreyLeftPanel">Email Address * <br />
<input name="emailaddress" type="text" class="contactRightPanel" size="25" />
</span></td>
</tr>
<tr>
<td width="465" height="43" align="left" valign="top" class="darkGreyLeftPanel">Phone Number * <br />
<input name="phone1" type="text" class="contactRightPanel" size="20" />
</span></td>
</tr>
<tr>
<td width="465" height="40" align="left" valign="top" class="darkGreyLeftPanel">Address #1 (Street number) *<br />
<input name="address1" type="text" class="contactRightPanel" size="55" />
</span></td>
</tr>
<tr>
<td width="465" height="40" align="left" valign="bottom" class="darkGreyLeftPanel">Address #2 (apt #, suite #, etc) * <br />
<input name="address2" type="text" class="contactRightPanel" size="55" />
</span></td>
</tr>
<tr>
<td width="465" height="40" align="left" valign="top"><table width="465" height="44" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="147" height="44" align="left" valign="bottom" class="darkGreyLeftPanel">City *<br />
<input name="city" type="text" class="contactRightPanel" size="20" />
</span></td>
<td width="149" height="44" align="left" valign="bottom" class="darkGreyLeftPanel">State *<br />
<input name="state" type="text" class="contactRightPanel" size="20" />
<br />
</span></td>
<td width="179" height="44" align="left" valign="bottom" class="darkGreyLeftPanel">Zip Code *<br />
<input name="zipcode" type="text" class="contactRightPanel" size="20" />
</span></td>
</tr>
</table></td>
</tr>
<tr>
<td width="465" align="right" valign="bottom"><input type="submit" value="Submit" name="submit" ><br />
</a></td>
</tr>
</table>
</form>
</div>
<div class="sidebar" id="content-related">


[/code]
Copy linkTweet thisAlerts:
@ayveghOct 29.2008 — Try this:
[code=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>
<title>Associa&ccedil;&atilde;o Brasileira Incentivo - Contato</title>
<link media="screen" type="text/css" rel="stylesheet" href="/resources/css/homepage.css"/>
<link media="screen" type="text/css" rel="stylesheet" href="/resources/css/diagram.css"/>
<link media="screen" type="text/css" rel="stylesheet" href="/resources/css/view.css"/>
<script type="text/javascript" src="/resources/scripts/hover.js"></script>
</head>

<body id="quem-somos">
<div id="banner">
<div id="nav-global-container">
<ul id="nav-global">
<li id="nav-contact"><a href="/contact/">Contato</a></li><li>|</li>
<!-- <li id="nav-history"><a href="/historia/">Histori&aacute;</a></li><li>|</li> -->
<li id="nav-background"><a href="/quem-somos/">Quem somos</a></li><li>|</li>
<li id="nav-home"><a href="/index.html">Home</a></li>
</ul>
</div>
</div>

<div id="container">

<div id="header">

<h1 id="logo"><a href="/index.html"><img alt="Incentivo" src="/resources/images/logo.png"/></a></h1>

<div id="nav-sub-container">
<ul id="nav-sub">
<li id="first"><div>Programas</div>
<ul>
<li><a href="/bolsa-de-estudo/">Bolsa de Estudo</a></li>
<li><a href="/financas-pessoais/">Finan&ccedil;as Pessoais</a></li>
<li><a href="/preparacao-para-mba/">Prepara&ccedil;&atilde;o para MBA</a></li>
</ul>
</li>
<li><div>Filosofia</div>
<ul>
<li><a href="/passar-adiante/">Passar adiante</a></li>
<li><a href="/successo/">Hist&oacute;ria de successo</a></li>
<li><a href="/voluntarios/">Volunt&aacute;rios</a></li>
</ul>
</li>
<!--
<li id="last"><div>Forums</div>
<ul>
<li><a href="/forums/">Bolsa</a></li>
<li><a href="/forums/">Financas</a></li>
<li><a href="/forums/">MBA</a></li>
</ul>
</li>
-->
</ul>
</div>
</div>

<div id="billboard"><h2>Incentivo</h2></div>

<div id="columnwrap">

<div id="content-main">

<h1>

Contato</h1>
<p><span class="contactLeftPanel">Your comments and concerns are important to us. Please provide the following information so that we can address your needs. We will email a response to you within three business days.</span></p>
<p><span class="contactLeftPanel"><span class="contactAsteriskIndicator">* Indicates required information</span></span></p>

<form method="post" action="/resources/scripts/mailer.php">


<table width="465" height="572" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="465" height="50" align="left" valign="top" class="darkGreyLeftPanel">My Question is for * <br />
<select name="questiondropdown" size="1" class="contactRightPanel">
<option value="-1">-- Select One --</option>
<?php
foreach ($departments as $id => $info)
{
print('<option value="');
print($id);
print('">');
print($info[0]);
print('</option>');
print("n");
}
?>
</select>
</td>
</tr>
<tr>
<td width="465" height="45" align="left" valign="top" class="darkGreyLeftPanel">Subject * <br />
<input name="subject" type="text" class="contactRightPanel" size="25" />
</td>
</tr>
<tr>
<td width="465" height="79" align="left" valign="top" class="darkGreyLeftPanel">Message | Comments *<br />
<textarea name="message" cols="75" rows="3" class="contactRightPanel"></textarea></td>
</tr>
<tr>
<td width="465" height="40" align="left" valign="top" class="darkGreyLeftPanel">What is your prefered method of contact? <br />
<table width="465" height="25" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="40" height="25" align="left" valign="middle" class="darkGreyLeftPanel">Email</td>
<td width="40" height="25" align="left" valign="middle" class="darkGreyLeftPanel"><input type="checkbox" name="check[]" value="Email"></td>
<td width="40" height="25" align="left" valign="middle" class="darkGreyLeftPanel">Phone</td>
<td width="40" height="25" align="left" valign="middle" class="darkGreyLeftPanel"><input type="checkbox" name="check[]" value="Phone"></td>
<td width="125" height="25" align="left" valign="middle" class="darkGreyLeftPanel">No Response Required </td>
<td width="195" height="25" align="left" valign="middle" class="darkGreyLeftPanel"><input type="checkbox" name="check[]" value="No Response Required"></td>
</tr>
</table></td>
</tr>
<tr>
<td width="465" height="50" align="left" valign="top" background="images/contact/yourinformation.gif">&nbsp;</td>
</tr>
<tr>
<td width="465" height="40" align="left" valign="top"><table width="465" height="40" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="182" height="40" align="left" valign="top" class="darkGreyLeftPanel">First Name *
<input name="firstname" type="text" class="contactRightPanel" size="25" />
</td>
<td width="293" align="left" valign="top"><span class="darkGreyLeftPanel">Last Name * <br />
<input name="lastname" type="text" class="contactRightPanel" size="25" />
</span></td>
</tr>
</table></td>
</tr>
<tr>
<td width="465" height="45" align="left" valign="middle" class="darkGreyLeftPanel">Email Address * <br />
<input name="emailaddress" type="text" class="contactRightPanel" size="25" />
</span></td>
</tr>
<tr>
<td width="465" height="43" align="left" valign="top" class="darkGreyLeftPanel">Phone Number * <br />
<input name="phone1" type="text" class="contactRightPanel" size="20" />
</span></td>
</tr>
<tr>
<td width="465" height="40" align="left" valign="top" class="darkGreyLeftPanel">Address #1 (Street number) *<br />
<input name="address1" type="text" class="contactRightPanel" size="55" />
</span></td>
</tr>
<tr>
<td width="465" height="40" align="left" valign="bottom" class="darkGreyLeftPanel">Address #2 (apt #, suite #, etc) * <br />
<input name="address2" type="text" class="contactRightPanel" size="55" />
</span></td>
</tr>
<tr>
<td width="465" height="40" align="left" valign="top"><table width="465" height="44" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="147" height="44" align="left" valign="bottom" class="darkGreyLeftPanel">City *<br />
<input name="city" type="text" class="contactRightPanel" size="20" />
</span></td>
<td width="149" height="44" align="left" valign="bottom" class="darkGreyLeftPanel">State *<br />
<input name="state" type="text" class="contactRightPanel" size="20" />
<br />
</span></td>
<td width="179" height="44" align="left" valign="bottom" class="darkGreyLeftPanel">Zip Code *<br />
<input name="zipcode" type="text" class="contactRightPanel" size="20" />
</span></td>
</tr>
</table></td>
</tr>
<tr>
<td width="465" align="right" valign="bottom"><input type="submit" value="Submit" name="submit" ><br />
</a></td>
</tr>
</table>
</form>
</div>
<div class="sidebar" id="content-related">[/code]
×

Success!

Help @bdavey311 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...