...

View Full Version : Contact form with ability to change the reciever



Jan
10-04-2004, 07:16 PM
Hey!

I've been looking around the net for this thing that I'd need for my website. You see, what I'd want to achieve is a form, where the person who is filling it out could choose between one of several different recievers. Like, he/she can send his/her message to person A, person B or person C by choosing one from a dropdown menu. Then the person would add his/her e-mail address, a topic and the message. Nothing fancier than that. Could someone be so kind and help me out on this? Thanks!


~Jan

sad69
10-04-2004, 09:26 PM
I'm assuming you're able to send mail to some hard-coded email address at present (based on scanactive.com).

The only difference would be:
1. You'd have an addition form element, a SELECT, with the various email addresses / receivers in it.
2. Alter the mail() call in send.php and replace the hard-coded email address with the value submitted from the SELECT.

You can hard-code the SELECT with the receivers, or you can store the receivers in a DB and populate the SELECT dynamically using PHP. Storing the receivers in a DB allows you to hide the receivers' email addresses, as instead of putting the email address as a value, you can put the ID number. Then before the call to mail() in send.php, grab the email address out of the DB using the ID number...

Let me know if you have any trouble with that.
Sadiq.

Celtboy
10-05-2004, 03:41 AM
Do you mean something like this:



<?php

function populate_recipient_select() {
$to_send = "";
$recipients_array = array(
"Recipient 1" => "recip1@example.com",
"Recipient 2" => "recip2@example.com",
"Recipient 3" => "recip3@example.com",
"Recipient 4" => "recip4@example.com"
);

foreach ($recipients_array as $name => $address) {
$to_send .= "<option value=\"" . $name . "&lt;" . $address . "&gt; \" >" . $name . "</option>\n";
}

return $to_send;
}

if (isset($_POST["sender"])) {

$recipient = $_POST["recipient"];
$sender = $_POST["sender"];
$subject = $_POST["subject"];
$message = $_POST["message"];
$reply_to = $_POST["sender_address"];

@mail($recipient, $subject, $message, "From: ". $sender . "Reply-To: " . $reply_address);
print "<html><body>Thank you for your submission!</body></html>";

} else {

?>

<html>
<head>
<title>Contact Form</title>
<style type="text/css">
<!--

html body { text-align: center; background-color:#666666}
h1 { font-size: 2em; }

.bold { font-weight: bold; }

#container {
text-align: left;
width: 450px;
margin: 0 auto;
border: solid #000000;
background-color:#ffffff;
}


#head { font-family: Garamond; text-align:center;}
#form { margin: 20px 20px 20px 20px; }

.lbl { font-family: Garamond; font-size: 1em; float:left; width: 150px; text-align:left; font-weight: bold; clear:leftt;}
.frmElement { width: 300px; text-align:left;}
.frmText { font-family: Garamond; color: #888888; font-size: 1em; background-color: #EEEEEE; margin: 0 0 15px 0;}
.frmSelect { font-family: Garamond; color: #888888; font-size: 1em; background-color: #EEEEEE; margin: 0 0 15px 0;}
.frmTextArea { font-family: Garamond; color: #888888 ; font-size: 1em; background-color: #EEEEEE; margin: 0 0 15px 0; text-align:top;}

#sub_container { width: 450px; margin: 0 -45px auto; text-align:right;}
.frmSubmit { font-family: Garamond; color: #000000; font-size: 1.2em; background-color: #EEEEEE; margin: 0 0 15px 0;}

//-->
</style>
<body>
<div id="container">
<div id="head"><h1>Contact Us</h1></head></div>
<div id="form">
<form action="<?php print $_SERVER['PHP_SELF']; ?>" method="POST">
<div class="lbl">Your Name:</div><div class="frmElement"><input type="text" name="sender" class="frmText" /></div>
<div class="lbl">Your Email:</div><div class="frmElement"><input type="text" name="sender_address" class="frmText" /></div>
<div class="lbl">Subject:</div><div class="frmElement"><input type="text" name="subject" class="frmText" /></div>
<div class="lbl">Select a Recipient:</div><div class="frmElement"><select name="recipient" class="frmSelect"><?php print populate_recipient_select();?></select></div>
<div class="lbl">Your Message:</div><div class="frmElement"><textarea class="frmTextArea bold" name="message" rows="15" cols="53"></textarea></div>

<div id="sub_container">
<input type="submit" name="submit" value="Submit" class="frmSubmit bold" />
</div>

</form>
</div>
</div>


</body>
</html>

<?php } ?>

-Celt

Jan
10-05-2004, 01:00 PM
Thanks for your help guys! Celtboy: I haven't had the chance to test out your script yet, but by the looks of it, it seems to do exactly what I asked for :) I'll try it out tomorrow and I'll let you know if I run into any problems, which I probably won't. Thanks!


~Jan

Jan
10-07-2004, 10:21 PM
Celtboy: Would it be possible to hide the e-mail addresses completly, so that they aren't visible for the visitor, not even in the source?


~Jan

sad69
10-07-2004, 10:49 PM
You might be able to if you morph your array into a 2D array:


$recipients_array = array();

$recipients_array[0] = array();
$recipients_array[0]['name'] = "Recipient 1";
$recipients_array[0]['email'] = "recip1@example.com";

$recipients_array[1] = array();
$recipients_array[1]['name'] = "Recipient 2";
$recipients_array[1]['email'] = "recip2@example.com";

$recipients_array[2] = array();
$recipients_array[2]['name'] = "Recipient 3";
$recipients_array[2]['email'] = "recip3@example.com";

$recipients_array[3] = array();
$recipients_array[3]['name'] = "Recipient 4";
$recipients_array[3]['email'] = "recip4@example.com";

function populate_recipient_select() {
global $recipients_array; //allow use of the recipients array...

$to_send = "";

foreach ($recipients_array as $idx => $info_array) {
$to_send .= "<option value=\"" . $idx . "\" >" . $recipients_array[$idx]['name'] . "</option>\n";
}

return $to_send;
}

if (isset($_POST["sender"])) {

$recipient = $recipients_array[$_POST["recipient"]]['email'];
$sender = $_POST["sender"];
$subject = $_POST["subject"];
$message = $_POST["message"];
$reply_to = $_POST["sender_address"];

@mail($recipient, $subject, $message, "From: ". $sender . "Reply-To: " . $reply_address);
print "<html><body>Thank you for your submission!</body></html>";

} else {


Untested, but that should do the trick.

If this list of recipients is going to grow and/or change, you may want to consider putting this information in a database, but the concept is the same..

Hope that helps/works,
Sadiq.

Celtboy
10-08-2004, 01:58 AM
also untested, but you should be able to just change the top of the code to this (the rest of the code stays the same):



<?php

function populate_recipient_select() {
$to_send = "";
$recipients_array = array(
"Recipient 1" => "recip1@example.com",
"Recipient 2" => "recip2@example.com",
"Recipient 3" => "recip3@example.com",
"Recipient 4" => "recip4@example.com"
);

foreach ($recipients_array as $name) {
$to_send .= "<option value=\"" . $name . "\" >" . $name . "</option>\n";
}

return $to_send;
}

if (isset($_POST["sender"])) {

$recipient = $_POST["recipient"];
$sender = $_POST["sender"];
$subject = $_POST["subject"];
$message = $_POST["message"];
$reply_to = $_POST["sender_address"];

@mail($recipients_array[$recipient], $subject, $message, "From: ". $sender . "Reply-To: " . $reply_address);
print "<html><body>Thank you for your submission!</body></html>";

} else {

?>


-Celt

Jan
10-08-2004, 10:04 AM
Thanks guys for your replies. However, there are some issues. First off, Celtboys suggestion still shows the e-mail addresses :( Look here: http://private.scanactive.com/experimental/multiplecontact1.php. Here's the code:



<?php

function populate_recipient_select() {
$to_send = "";
$recipients_array = array(
"Recipient 1" => "recip1@example.com",
"Recipient 2" => "recip2@example.com",
"Recipient 3" => "recip3@example.com",
"Recipient 4" => "recip4@example.com"
);

foreach ($recipients_array as $name) {
$to_send .= "<option value=\"" . $name . "\" >" . $name . "</option>\n";
}

return $to_send;
}

if (isset($_POST["sender"])) {

$recipient = $_POST["recipient"];
$sender = $_POST["sender"];
$subject = $_POST["subject"];
$message = $_POST["message"];
$reply_to = $_POST["sender_address"];

@mail($recipients_array[$recipient], $subject, $message, "From: ". $sender . "Reply-To: " . $reply_address);
print "<html><body>Thank you for your submission!</body></html>";

} else {

?>

<html>
<head>
<title>Contact Form</title>
<style type="text/css">
<!--

html body { text-align: center; background-color:#666666}
h1 { font-size: 2em; }

.bold { font-weight: bold; }

#container {
text-align: left;
width: 450px;
margin: 0 auto;
border: solid #000000;
background-color:#ffffff;
}


#head { font-family: Garamond; text-align:center;}
#form { margin: 20px 20px 20px 20px; }

.lbl { font-family: Garamond; font-size: 1em; float:left; width: 150px; text-align:left; font-weight: bold; clear:leftt;}
.frmElement { width: 300px; text-align:left;}
.frmText { font-family: Garamond; color: #888888; font-size: 1em; background-color: #EEEEEE; margin: 0 0 15px 0;}
.frmSelect { font-family: Garamond; color: #888888; font-size: 1em; background-color: #EEEEEE; margin: 0 0 15px 0;}
.frmTextArea { font-family: Garamond; color: #888888 ; font-size: 1em; background-color: #EEEEEE; margin: 0 0 15px 0; text-align:top;}

#sub_container { width: 450px; margin: 0 -45px auto; text-align:right;}
.frmSubmit { font-family: Garamond; color: #000000; font-size: 1.2em; background-color: #EEEEEE; margin: 0 0 15px 0;}

//-->
</style>
<body>
<div id="container">
<div id="head"><h1>Contact Us</h1></head></div>
<div id="form">
<form action="<?php print $_SERVER['PHP_SELF']; ?>" method="POST">
<div class="lbl">Your Name:</div><div class="frmElement"><input type="text" name="sender" class="frmText" /></div>
<div class="lbl">Your Email:</div><div class="frmElement"><input type="text" name="sender_address" class="frmText" /></div>
<div class="lbl">Subject:</div><div class="frmElement"><input type="text" name="subject" class="frmText" /></div>
<div class="lbl">Select a Recipient:</div><div class="frmElement"><select name="recipient" class="frmSelect"><?php print populate_recipient_select();?></select></div>
<div class="lbl">Your Message:</div><div class="frmElement"><textarea class="frmTextArea bold" name="message" rows="15" cols="53"></textarea></div>

<div id="sub_container">
<input type="submit" name="submit" value="Submit" class="frmSubmit bold" />
</div>

</form>
</div>
</div>


</body>
</html>

<?php } ?>


Then, to Sadiq's suggestion where there seems to be another problem: http://private.scanactive.com/experimental/multiplecontact2.php. The code:



<?php

function populate_recipient_select() {
$to_send = "";
$recipients_array = array();

$recipients_array[0] = array();
$recipients_array[0]['name'] = "Recipient 1";
$recipients_array[0]['email'] = "recip1@example.com";

$recipients_array[1] = array();
$recipients_array[1]['name'] = "Recipient 2";
$recipients_array[1]['email'] = "recip2@example.com";

$recipients_array[2] = array();
$recipients_array[2]['name'] = "Recipient 3";
$recipients_array[2]['email'] = "recip3@example.com";

$recipients_array[3] = array();
$recipients_array[3]['name'] = "Recipient 4";
$recipients_array[3]['email'] = "recip4@example.com";

function populate_recipient_select() {
global $recipients_array; //allow use of the recipients array...

$to_send = "";

foreach ($recipients_array as $idx => $info_array) {
$to_send .= "<option value=\"" . $idx . "\" >" . $recipients_array[$idx]['name'] . "</option>\n";
}

return $to_send;
}

if (isset($_POST["sender"])) {

$recipient = $recipients_array[$_POST["recipient"]]['email'];
$sender = $_POST["sender"];
$subject = $_POST["subject"];
$message = $_POST["message"];
$reply_to = $_POST["sender_address"];

@mail($recipient, $subject, $message, "From: ". $sender . "Reply-To: " . $reply_address);
print "<html><body>Thank you for your submission!</body></html>";

} else {

?>

<html>
<head>
<title>Contact Form</title>
<style type="text/css">
<!--

html body { text-align: center; background-color:#666666}
h1 { font-size: 2em; }

.bold { font-weight: bold; }

#container {
text-align: left;
width: 450px;
margin: 0 auto;
border: solid #000000;
background-color:#ffffff;
}


#head { font-family: Garamond; text-align:center;}
#form { margin: 20px 20px 20px 20px; }

.lbl { font-family: Garamond; font-size: 1em; float:left; width: 150px; text-align:left; font-weight: bold; clear:leftt;}
.frmElement { width: 300px; text-align:left;}
.frmText { font-family: Garamond; color: #888888; font-size: 1em; background-color: #EEEEEE; margin: 0 0 15px 0;}
.frmSelect { font-family: Garamond; color: #888888; font-size: 1em; background-color: #EEEEEE; margin: 0 0 15px 0;}
.frmTextArea { font-family: Garamond; color: #888888 ; font-size: 1em; background-color: #EEEEEE; margin: 0 0 15px 0; text-align:top;}

#sub_container { width: 450px; margin: 0 -45px auto; text-align:right;}
.frmSubmit { font-family: Garamond; color: #000000; font-size: 1.2em; background-color: #EEEEEE; margin: 0 0 15px 0;}

//-->
</style>
<body>
<div id="container">
<div id="head"><h1>Contact Us</h1></head></div>
<div id="form">
<form action="<?php print $_SERVER['PHP_SELF']; ?>" method="POST">
<div class="lbl">Your Name:</div><div class="frmElement"><input type="text" name="sender" class="frmText" /></div>
<div class="lbl">Your Email:</div><div class="frmElement"><input type="text" name="sender_address" class="frmText" /></div>
<div class="lbl">Subject:</div><div class="frmElement"><input type="text" name="subject" class="frmText" /></div>
<div class="lbl">Select a Recipient:</div><div class="frmElement"><select name="recipient" class="frmSelect"><?php print populate_recipient_select();?></select></div>
<div class="lbl">Your Message:</div><div class="frmElement"><textarea class="frmTextArea bold" name="message" rows="15" cols="53"></textarea></div>

<div id="sub_container">
<input type="submit" name="submit" value="Submit" class="frmSubmit bold" />
</div>

</form>
</div>
</div>


</body>
</html>

<?php } ?>


Have I done this the right way, or what?


~Jan

Celtboy
10-08-2004, 03:01 PM
Sorry about that. I shouldn't have used $name, and the recipient's list needs to be global. Try the following:

try using this code:


<?php

$recipients_array = array(
"Recipient 1" => "recip1@example.com",
"Recipient 2" => "recip2@example.com",
"Recipient 3" => "recip3@example.com",
"Recipient 4" => "recip4@example.com"
);

function populate_recipient_select($recipients_array) {


$to_send = "";


foreach ($recipients_array as $recip_name=>$value) {
$to_send .= "<option value=\"" . $recip_name . "\" >" . $recip_name . "</option>\n";
}

return $to_send;
}

if (isset($_POST["sender"])) {

$recipient = $_POST["recipient"];
$sender = $_POST["sender"];
$subject = $_POST["subject"];
$message = $_POST["message"];
$reply_to = $_POST["sender_address"];

@mail($recipients_array[$recipient], $subject, $message, "From: ". $sender . "\r\nReply-To: " . $reply_address);
print "<html><body>Thank you for your submission!</body></html>";

} else {

?>

<html>
<head>
<title>Contact Form</title>
<style type="text/css">
<!--

html body { text-align: center; background-color:#666666}
h1 { font-size: 2em; }

.bold { font-weight: bold; }

#container {
text-align: left;
width: 450px;
margin: 0 auto;
border: solid #000000;
background-color:#ffffff;
}


#head { font-family: Garamond; text-align:center;}
#form { margin: 20px 20px 20px 20px; }

.lbl { font-family: Garamond; font-size: 1em; float:left; width: 150px; text-align:left; font-weight: bold; clear:leftt;}
.frmElement { width: 300px; text-align:left;}
.frmText { font-family: Garamond; color: #888888; font-size: 1em; background-color: #EEEEEE; margin: 0 0 15px 0;}
.frmSelect { font-family: Garamond; color: #888888; font-size: 1em; background-color: #EEEEEE; margin: 0 0 15px 0;}
.frmTextArea { font-family: Garamond; color: #888888 ; font-size: 1em; background-color: #EEEEEE; margin: 0 0 15px 0; text-align:top;}

#sub_container { width: 450px; margin: 0 -45px auto; text-align:right;}
.frmSubmit { font-family: Garamond; color: #000000; font-size: 1.2em; background-color: #EEEEEE; margin: 0 0 15px 0;}

//-->
</style>
<body>
<div id="container">
<div id="head"><h1>Contact Us</h1></head></div>
<div id="form">
<form action="<?php print $_SERVER['PHP_SELF']; ?>" method="POST">
<div class="lbl">Your Name:</div><div class="frmElement"><input type="text" name="sender" class="frmText" /></div>
<div class="lbl">Your Email:</div><div class="frmElement"><input type="text" name="sender_address" class="frmText" /></div>
<div class="lbl">Subject:</div><div class="frmElement"><input type="text" name="subject" class="frmText" /></div>
<div class="lbl">Select a Recipient:</div><div class="frmElement"><select name="recipient" class="frmSelect"><?php print populate_recipient_select($recipients_array);?></select></div>
<div class="lbl">Your Message:</div><div class="frmElement"><textarea class="frmTextArea bold" name="message" rows="15" cols="53"></textarea></div>

<div id="sub_container">
<input type="submit" name="submit" value="Submit" class="frmSubmit bold" />
</div>

</form>
</div>
</div>


</body>
</html>

<?php } ?>

-Celt
[/php]

Jan
10-08-2004, 05:15 PM
Yeah, it works great now! :thumbsup: Thanks, Celtboy!


~Jan

Jan
10-12-2004, 03:28 PM
Hi again! I'd have one more question if you guys don't mind. Is there any way to have the message previewed before sending it? I mean so that the user can check that everything is correct? No biggie, but would be a cool extra ;)


~Jan



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum