0

我有一个我使用的邮件表格。在新用法中,我添加了 2 个复选框。我希望能够根据复选框将邮件发送到不同的地址。EX: if "box1 check mail to xx, if box 2 check mail to xx, else mail to both.我用来邮寄的代码是:

<?

/************************
* Variables you can change
*************************/

$mailto = "";
$cc = "";
$bcc = "";
$subject = "contact";
$vname = "Enquiry";


/************************
* do not modify anything below unless you know PHP/HTML/XHTML
*************************/
/* my trial adjustment for check boxes
*/


$email = $_POST['email'];

function validateEmail($email)
{
   if(eregi('^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z]{2,4}(\.[a-zA-Z]{2,3})?(\.[a-zA-Z]{2,3})?$', $email))
      return true;
   else
      return false;
}


if((strlen($_POST['name']) < 1 ) || (strlen($email) < 1 ) || (strlen($_POST['message']) < 1 ) || validateEmail($email) == FALSE){
    $emailerror .= '';

    if(strlen($_POST['name']) < 1 ){
        $emailerror .= '<li>Enter name</li>';
    }

    if(strlen($email) < 1 ){
        $emailerror .= '<li>Enter email</li>';
    }

    if(validateEmail($email) == FALSE) {
        $emailerror .= '<li>Enter valid email</li>';
    }

    if(strlen($_POST['message']) < 1 ){
        $emailerror .= '<li>Enter message</li>';
    }

} else {

    $emailerror .= "Your email has been sent successfully";



    // NOW SEND THE ENQUIRY

    $timestamp = date("F j, Y, g:ia");

    $messageproper ="\n\n" .
        "Name: " .
        ucwords($_POST['name']) .
        "\n" .
        "Email: " .
        ucwords($email) .
        "\n" .
        "Telephone Contact: " .
        ucwords($_POST['tel']) .
        "\n" .
        "Company: " .
        ucwords($_POST['company']) .
        "\n" .
        "Comments: " .
        $_POST['message'] .
        "\n" .
        "\n\n" ;

        $messageproper = trim(stripslashes($messageproper));
        mail($mailto, $subject, $messageproper, "From: \"$vname\" <".$_POST['e_mail'].">\nReply-To: \"".ucwords($_POST['first_name'])."\" <".$_POST['e_mail'].">\nX-Mailer: PHP/" . phpversion() );

}
?>

<div id='emailerror'>
    <ul>
        <? echo $emailerror; ?>
    </ul>
</div>

复选框代码:

   <label>MailPersonA</label>
    <input type="checkbox" name="A" value="checkbox" id="A" />
   <label>MailPersonB</label>
      <input type="checkbox" name="B" value="checkbox" id="B" />

任何帮助将不胜感激。

4

2 回答 2

2

你可能会做得更好:

   <label>MailPersonA</label>
    <input type="checkbox" name="emailadd[]" value="email1@example.com" id="A" />
   <label>MailPersonB</label>
      <input type="checkbox" name="emailadd[]" value="email2@example.com" id="B" />

然后在处理中:

if($_POST['emailadd']) {
    foreach($_POST['emailadd'] as $email) {
         $mailto.= $email . ',';
    }

$mailto= trim($mailto,',');

根据表单中的值在后端添加电子邮件可能是一个好主意,而不是像我在这里所做的那样将它们正确地放在表单中。我只是为了举例而简化。

于 2012-11-01T02:35:28.800 回答
0

您可能想要:

  • 使用单选输入而不是复选框,以确保准确选择了一个
  • 将收音机命名为相同
  • 给他们不同的价值

所以你的 HTML 将是

<label>MailPersonA</label>
<input type="radio" name="toaddress" value="person1@wherever.com" id="A" />
<label>MailPersonB</label>
<input type="radio" name="toaddress" value="person2@wherever.com" id="B" />

现在$_POST['toaddress']将包含目标电子邮件地址。

于 2012-11-01T02:40:14.057 回答