在具有多个收件人的php表单中出现'if statement'

时间:2014-01-08 01:35:48

标签: html php

我的php表单为多个收件人提供<select>选项,其中每个收件人都有一个与其相关联的电子邮件地址:

<select name="sendto">
  <option value="select">-- Select --</option>
  <option value="general">General</option>
  <option value="support">Support</option>
  <option value="sales">Sales</option>
</select>

我只能成功发送和接收“常规”选项,其余选项失败。显而易见的问题是' if语句 ',但我无法弄清问题究竟是什么。有帮助吗?感谢。

这是php代码:

<?php 
 $to = $_REQUEST['sendto'] ; 
 $from = $_REQUEST['Email'] ; 
 $name = $_REQUEST['Name'] ; 
 $headers = "From: $from"; 
 $subject = "Web Contact Data"; 

 $fields = array(); 
 $fields{"Name"} = "Name"; 
 $fields{"Company"} = "Company"; 
 $fields{"Email"} = "Email"; 
 $fields{"Phone"} = "Phone"; 
 $fields{"list"} = "Mailing List"; 
 $fields{"Message"} = "Message"; 

 $body = "We have received the following information:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); } 

 $headers2 = "From: noreply@my-site.com"; 
 $subject2 = "Thank you for contacting us"; 
 $autoreply = "Thank you for contacting us. Somebody will get back to you as soon as possible, usualy within 48 hours. If you have any more questions, please consult our website at www.my-site.com";


if ($sendto == 'general') { 
    $to = 'alex@my-site.com';
}
else if ($sendto == 'support') { 
    $to = 'alabanino@my-site.com'; 
}
else if ($sendto == 'sales') { 
    $to = 'alabanino@my-site.com'; 
}
else { //other options
    $to = 'alex@my-site.com'; 
}

 if($from == '') { print "You have not entered an email, please go back and try again"; } 
    else { 
 if($name == '') { print "You have not entered a name, please go back and try again"; } 
     else { 
 $send = mail($to, $subject, $body, $headers, '-fnoreply@yourmailer.com'); 
 $send2 = mail($from, $subject2, $autoreply, $headers2); 
 if($send){
     header( "Location: http://www.my-site.com/thankyou.html" );} 
 else {
     print "We encountered an error sending your mail, please notify webmaster@my-site.com"; 
   } 
 }
}
 ?>

1 个答案:

答案 0 :(得分:2)

$ sento定义在哪里?这将是“有效”因为它将违反你的最终其他条件,因为所有其他人都失败了。

你需要添加它,它应该工作

$sendto = $_REQUEST['sendto']

或者使用$to insetad