I have this simple php form which works, my problem is that it takes the user to a new page with, a single message once submitted. I’m pretty new to php and have pieced this together. Can anybody help me add an array of subject lines that corresponds with the email selected from the dropdown. I’m also looking to clear the contact box and add some ‘your message has been sent’ text above the box itself rather then change the page. Can anybody help me out?
[code=php]<?php
// config
$emailAddresses = array(
‘Sales’=>’[email protected]’,
‘Advertising’=>’[email protected]’,
);
$emailSubject = ‘The subject line of the email’;
// If we are dealing with a form submission, send an email
if (isset($_POST[‘name’])) {
// Check all the fields are present
if (!isset($_POST[‘destemail’],$_POST[‘comment’],$_POST[‘srcemail’])) {
exit(“Sorry, at least one of the POST fields is missing.”);
}
// Check the email selected is valid
if (!isset($emailAddresses[$_POST[‘destemail’]])) {
exit(“Sorry, you have selected an invalid email option.”);
}
// Create the body of the email message
$emailBody = “Dear {$_POST[‘destemail’]}nn{$_POST[‘name’]} ({$_POST[‘srcemail’]}) said:nn{$_POST[‘comment’]}nnBest RegardsnnSome random website”;
// Send the email and report the result
if (mail($emailAddresses[$_POST[‘destemail’]],$emailSubject,$emailBody,”From: {$_POST[‘srcemail’]}”)) {
exit(“Email sent successfully”);
} else {
exit(“Email sending failed”);
}
}
// Output the html form
?>
[code=html]<form action=”” method=”POST”>
<table style=”border-width: 0px; border-style: none;”>
<tr>
<td valign=”top”>Department</td>
<td><select name=”destemail”>
<?php foreach ($emailAddresses as $name => $email) { ?>
<option value=”<?php echo htmlspecialchars($name); ?>”><?php echo htmlspecialchars($name); ?></option>
<?php } ?>
</select> </td>
</tr>
<tr>
<td valign=”top”>Name</td>
<td><input type=”text” name=”name” /></td>
</tr>
<tr>
<td valign=”top”>Email</td>
<td><input type=”text” name=”srcemail” /></td>
</tr>
<tr>
<td valign=”top”>Message:</td>
<td><textarea cols=”35″ rows=”8″ name=”comment”></textarea></td>
</tr>
<tr>
<td colspan=”2″><input type=”submit” /></td>
</tr>
</table>
</form>