I have a checkbox form where different check boxes need to send the form to different email addresses. Right now, no matter the check box, it goes to the same emails.

  <span class="clients">
            <input type="checkbox" name="blah" value="checkbox" />
            Blah</span></p>
           <p>
             <span class="clients">

going here:

<SCRIPT LANGUAGE="php">
    $email = $HTTP_POST_VARS[email];
    $mailto ="you@you.com,me@you.com";
    $mailsubj = "Quote Request";
    $mailhead = "From: nate@therussogroup.com";
    reset ($HTTP_POST_VARS);
    $mailbody = "Values submitted from web site form:\n";
    while (list ($key, $val) = each ($HTTP_POST_VARS)) {
        $mailbody .= "$key : $val\n"; }
    mail($mailto, $mailsubj, $mailbody, $mailhead);
</SCRIPT>

A. If u had a javascript on your form, that generates the '$mailto' values based on the checkbox 'checked' property, it might make life easier for you - with validation possibilities.

B. For the moment the '$mailto' is hard coded and no decision making is made to define the email recipients. No wonder it is going to the same emails whatever be the checkbox combinations.

C. In case you have a lot of checkbox and combinations, it is practical to standardize your checkbox ids/names and a simple loop in the client or server script would easily define the email combination you require.

Hope the above tips help.

What you say makes sense but I don't really know php at all. I was hoping there would just be a few lines of code I could copy & pop in. So, it's more difficult than that? Thanks.

It's not really difficult . All you need is just a loop to go through all the checked boxes, and generate/concatenate the $mailto recipient list.

how are the checkbox names? If the loop does not work, the easiest but long way is to have a list of if statements, generating/concatenating the $mailto recipient list for you.

Okie, I'm sending you a private message on the email add provided. Please confirm receipt.

This is mail.html. Here you can add the checkboxes specifying the email address as values to those checkboxes.

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Send mail</title>
</head>

<body>
<form action="mail.php" method="post">
<INPUT TYPE="checkbox" NAME="chk1" value="example1@example.com">Name 1 <br />
<INPUT TYPE="checkbox" NAME="chk2" value="example2@example.com">Name 2 <br />
<INPUT TYPE="checkbox" NAME="chk3" value="example3@example.com">Name 3 <br />
<INPUT TYPE="checkbox" NAME="chk4" value="example4@example.com">Name 4 <br />
<INPUT TYPE="checkbox" NAME="chk5" value="example5@example.com">Name 5 <br />
<INPUT TYPE="checkbox" NAME="chk6" value="example6@example.com">Name 6 <br />
<INPUT TYPE="checkbox" NAME="chk7" value="example7@example.com">Name 7 <br />
<input type="submit" value="Submit" name="submit">
</form>
</body>
</html>

This is mail.php. Here you loop through all the posted variables, check if its value is submit, if it is submit, then skip it, else, send a mail !

<?php //mail.php
foreach ($_POST as $key => $value){
	if($value !="Submit"){
		$to = $value;
		$email="noreply@yoursite.com";
		$subject="Test mail !!!!";
		$message = "Hey ! I dont have any message !";
		mail($to,$subject,$message,"From:".$email);
	}
}
?>

Cheers,
Naveen

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.