/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] php contact form

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]

[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>[/code]

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@VBAssassinMay 31.2012 — There is nothing you don't already know PHP and HTML wise in order for you to create a solution to this problem.

All you need are a few more variables, few more if's and elses, and a few print statements ?

Don't give up and ask for help at the first sign of trouble. I'll help you out when you try some more.

Kind regards,

Scott
Copy linkTweet thisAlerts:
@absolutmgd13authorMay 31.2012 — i just logged in to mark this resolved. i guess i was just having a brain fart! one thing i can't get right is the email validation from the person submitting. got any suggestions? btw, i pieced that together from examples, i didn't write it.
Copy linkTweet thisAlerts:
@VBAssassinJun 02.2012 — Yeah, use regular expressions to validate the email address is in the correct format.

Google will give you a million examples.

Kind regards,

Scott
Copy linkTweet thisAlerts:
@kbduvallJun 03.2012 — I try to use built in functions for validation where I can. PHP has decent email validation with filter_var
[code=php]
$is_valid = filter_var($email, FILTER_VALIDATE_EMAIL);
[/code]
×

Success!

Help @absolutmgd13 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.28,
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,
)...