Forum Moderators: coopster

Message Too Old, No Replies

multiple recipients via checkbox

using checkboxes to select multiple recipients

         

skhla

5:01 pm on May 20, 2006 (gmt 0)

10+ Year Member



This is my first try at php. The form works when I have just one recipient. But I don't have the code right for having multiple recipients selected by using checkboxes. Any help would be hugely appreciated! Below is the code for formpage.php, and mail.php.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Form</title>
</head>
<body>
<form action="mail.php" method="post">
Your Name:<br>
<input type="text" name="name"><br><br>
E-mail:<br>
<input type="text" name = "email"><br><br>
Comments<br>
<textarea name="comments"></textarea><br><br>
<input type="checkbox" name="incoming_mailto[]" value="gmail">Send form to gmail address</input><br>
<input type="checkbox" name="incoming_mailto[]" value="yahoo">Send form to yahoo address</input><br>
<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

<?
function checkOK($field)
{
if (eregi("\r",$field) ¦¦ eregi("\n",$field)){
die("Invalid Input!");
}
}
// make array of all possible mails that could be used - a security check
$permittedmailsarray = Array(
'gmail' => 'skharrisla@gmail.com',
'yahoo'=> 'webtechla@yahoo.com'
);
if(is_array($_POST['incoming_mailto'])) {
foreach($_POST['incoming_mailto'] as $v){
$recipients = $permittedmailsarray[$v];
}
}
$name=$_POST['name'];
checkOK($name);
$email=$_POST['email'];
checkOK($email);
$comments=$_POST['comments'];
checkOK($comments);
$to="skh@webtechla.com";
$message="$name just filled in your comments form. They said:\n$comments\n\nTheir e-mail address was: $email";
if(mail($to,"Comments From Your Site",$message,"From: $email\n")) {
echo "Thanks for your feedback.";
} else {
echo "There was a problem sending the mail. Please check that you filled in the form correctly.";
}
?>

dreamcatcher

5:19 pm on May 20, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



skhla, a warm welcome to Webmaster World. :)

This line you have in your foreach loop:

$recipients = $permittedmailsarray[$v];

Whats happening there is you are assigning a variable and then over writing it each time the loop loops. So, the value will only be the value of the last iteration.

What are you trying to do exactly? Do you mind me asking? Prevent spamming in some way?

dc

skhla

3:13 am on May 21, 2006 (gmt 0)

10+ Year Member



The form is for residents to send comments to the city government offices. After typing their comments, we want users to be able to click in checkboxes to designate which city offices should receive the email. We've been using formmail at our site. But our host says we must use php only beginning next month. Again, we really, really appreciate any help. Thanks so much for replying.

dreamcatcher

7:47 am on May 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok, well you can change this:


foreach($_POST['incoming_mailto'] as $v){
$recipients = $permittedmailsarray[$v];
}

to this:


$recipients = array();
foreach($_POST['incoming_mailto'] as $v){
$recipients[] = $permittedmailsarray[$v];
}

and then implode the array in your mail function:


if (!empty($recipients))
{
$to_field = implode(",", $recipients);
if(mail($to_field,"Comments From Your Site",$message,"From: $email\n")) {
echo "Thanks for your feedback.";
} else {
echo "There was a problem sending the mail. Please check that you filled in the form correctly.";
}
}

Does that help at all?

dc

skhla

11:04 am on May 22, 2006 (gmt 0)

10+ Year Member



It works perfectly. Thanks so much!