【问题标题】:Specific email conditional on Radio Button selected in Form特定电子邮件以表单中选择的单选按钮为条件
【发布时间】:2016-05-15 20:39:48
【问题描述】:

感谢您在此处提供的所有信息,但我似乎缺少将其发送到每个特定电子邮件地址的内容。

此代码的基础来自线程: Send PHP Form to Different Emails Based on Radio Buttons

<form name="contact-form" method="post" action="contact.php">
  <div class="form_details">
    <input type="text" id="field_name" name="sender_name" class="text" value="name">
    <input type="text" id="field_email" name="sender_email" class="text" value="email" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Email';}">                                     
    <input type="text" id="field_subject" name="sender_subject" class="text" value="subject" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Subject';}">                                     
    <textarea id="field_message" name="sender_message" value="Message" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Message';}"></textarea>
    <div class="clearfix"> </div>
    <div class="sub-button">
      <div style="float:left">
        <input type="radio" name="department" value="archery"> Archery
        <input type="radio" name="department" value="firearms"> Firearms
        <input type="radio" name="department" value="general"> General Inquiry
      </div>
    <div class="g-recaptcha" data-sitekey="6LeydxcTAAAAACBd4beoLDgtu0LIqzTWOJnvfsH-"></div>
      <input type="submit" name="send_message" value="Submit">
    </div>
  </div>
</form>

<?php
  // Assigning data from the $_POST array to variables     
  $name = $_POST['sender_name'];     
  $mail_from = $_POST['sender_email'];     
  $phone = $_POST['sender_phone'];
  $sender_subject = $_POST['sender_subject'];          
  $message = $_POST['sender_message'];     

  $radio = isset($_POST['archery']) ? $_POST['firearms'] : $_POST['general'];
  if (isset($_POST['archery'])) {
    $department = 'info1@info.com';
  } elseif (isset($_POST['firearms'])) {
    $department = 'info2@info.com';
  } elseif (isset($_POST['general'])) {
    $department = 'info3@info.com';
  } else {
    $department = 'info4@info.com';
  }

  // Construct email subject     
  $subject = 'SC Website Message ';     

  // Construct email body     
  $body_message .= 'From: ' . $name . "\r\n";     
  $body_message .= 'E-mail: ' . $mail_from . "\r\n";
  $body_message .= 'Subject: ' . $sender_subject . "\r\n";     
  $body_message .= 'Message: ' . $message;     

  // Construct email headers     
  $headers = 'From: ' . $mail_from . "\r\n";     
  $headers .= 'Reply-To: ' . $mail_from . "\r\n";     

  $mail_sent = mail($mail_to, $subject, $body_message, $headers);     
  if ($mail_sent == true){ ?>         
    <script language="javascript" type="text/javascript">         
      alert('Thank you for your message, we will be in contact shortly.');         
      window.location = 'contact.html';         
    </script>     
  <?php } else { ?>     
    <script language="javascript" type="text/javascript">         
      alert('Message not sent. Please, notify the site administrator     info1@info.com');         
      window.location = 'contact.html';     
    </script>     
  <?php     
  } 
?>

【问题讨论】:

  • 没有你的 HTML 没有人能知道发生了什么。您是否查看了为链接问题提供的答案?特别是接受的答案?
  • 没有足够的代码来支持这个问题;主要是您正在使用的 HTML 表单。
  • 另外,$mail_to 在此处未定义。
  • HTML 添加在上面,不确定它是否是最好的方法,但有效

标签: php forms button conditional radio


【解决方案1】:

编辑:

HTML

<form name="contact-form" method="POST" action="contact.php">
<div class="form_details">
<input type="text" id="field_name" name="sender_name" class="text" placeholder="name"/>
<input type="text" id="field_email" name="sender_email" class="text" value="email" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Email';}"/>                                     
<input type="text" id="field_subject" name="sender_subject" class="text" value="subject" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Subject';}"/>                                     
<textarea id="field_message" name="sender_message" placeholder="Message" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Message';}"></textarea>
<div class="clearfix"> </div>
<div class="sub-button">
<div style="float:left">
<input type="radio" name="department" value="archery"/> Archery
<input type="radio" name="department" value="firearms"/> Firearms
<input type="radio" name="department" value="general"/> General Inquiry
</div>
<div class="g-recaptcha" data-sitekey="6LeydxcTAAAAACBd4beoLDgtu0LIqzTWOJnvfsH-"></div>
<input type="submit" name="send_message" value="Submit"/>
</div>
</div>
</form>

PHP

    <?php
    if (isset($_POST["send_message"])) {
    $name = $_POST["sender_name"];
    $mail_from = $_POST["sender_email"];
    //$phone = $_POST["sender_phone"];
    $sender_subject = $_POST["sender_subject"];
    $message = $_POST["sender_message"];

    $radio = $_POST["department"];
    if ($radio == "archery") {
        $department = "info1@info.com";
    } elseif ($radio == "firearms") {
        $department = "info2@info.com";
    } elseif ($radio == "general") {
        $department = "info3@info.com";
    } else {
        $department = "info4@info.com";
    }

    // Construct email subject     
    $subject = 'SC Website Message ';     

    // Construct email body   
    $body_message = "";  
    $body_message .= 'From: ' . $name . "\r\n";     
    $body_message .= 'E-mail: ' . $mail_from . "\r\n";
    $body_message .= 'Subject: ' . $sender_subject . "\r\n";     
    $body_message .= 'Message: ' . $message;     

    // Construct email headers     
    $headers = 'From: ' . $mail_from . "\r\n";     
    $headers .= 'Reply-To: ' . $mail_from . "\r\n";     
    // As flow mail sent to department
    $mail_to=$department;
    $mail_sent = mail($mail_to, $subject, $body_message, $headers);

    if ($mail_sent == true){ ?>         
    <script language="javascript" type="text/javascript">         
    alert('Thank you for your message, we will be in contact shortly.');         
    window.location = 't1.php';         
    </script>     
    <?php } else { ?>     
    <script language="javascript" type="text/javascript">         
    alert('Message not sent. Please, notify the site administrator     info1@info.com');         
    window.location = 't1.php';     
    </script>     
   <?php     
    }
    }
    ?>

【讨论】:

  • 收音机都具有相同的“名称”,“值”不同......单选按钮选择工作正常。表单验证,只是不发送电子邮件。我的 PHP 能力非常有限
  • 你添加了 $mail_to 吗?
  • $mail_to 未分配
  • $mail_sent = mail($department, $subject, $body_message, $headers);
  • 返回原始代码并如上更改,98% 工作...除了选择射箭电台时,仍发送到“其他”电子邮件地址....而不是适当的射箭电子邮件。对不起,我知道如何解释它的最佳方式
【解决方案2】:
<form name="contact-form" method="post" action="demo.php">
<div class="form_details">
    <input type="text" id="field_name" name="sender_name" class="text" value="name">
    <input type="text" id="field_email" name="sender_email" class="text" value="email" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Email';}">                                     
    <input type="text" id="field_subject" name="sender_subject" class="text" value="subject" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Subject';}">                                     
    <textarea id="field_message" name="sender_message" value="Message" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Message';}"></textarea>
    <div class="clearfix"> </div>
    <div class="sub-button">
    <div style="float:left">
<input type="radio" name="department" value="archery"> Archery
<input type="radio" name="department" value="firearms"> Firearms
<input type="radio" name="department" value="general"> General Inquiry
</div>
<div class="g-recaptcha" data-sitekey="6LeydxcTAAAAACBd4beoLDgtu0LIqzTWOJnvfsH-"></div>
<input type="submit" name="send_message" value="Submit">
</div>
</div>
</form>

<?php
// Assigning data from the $_POST array to variables     
if(isset($_POST['sender_name']) && isset($_POST['sender_email'] ) && isset($_POST['sender_phone']) && isset($_POST['sender_subject']) && isset($_POST['sender_message'])){
$name = $_POST['sender_name'];     
$mail_from = $_POST['sender_email'];     
$phone = $_POST['sender_phone'];
$sender_subject = $_POST['sender_subject'];          
$message = $_POST['sender_message'];    
/**********************Change Radio button name *******************/
$radio=null;
if(isset($_POST['department'])){

    $radio = $_POST['department'];

} 
if ($radio=='archery') {
  $department = 'info1@info.com';
} elseif ($radio=='firearms') {
  $department = 'info2@info.com';
} elseif ($radio=='general') {
  $department = 'info3@info.com';
} else {
  $department = 'info4@info.com';
}


// Construct email subject     
$subject = 'SC Website Message ';     

// Construct email body     
$body_message .= 'From: ' . $name . "\r\n";     
$body_message .= 'E-mail: ' . $mail_from . "\r\n";
$body_message .= 'Subject: ' . $sender_subject . "\r\n";     
$body_message .= 'Message: ' . $message;     

// Construct email headers     
$headers = 'From: ' . $mail_from . "\r\n";     
$headers .= 'Reply-To: ' . $mail_from . "\r\n";     
// As flow mail sent to department
$mail_to=$department;

$mail_sent = mail($mail_to, $subject, $body_message, $headers);     
if ($mail_sent == true){ ?>         
  <script language="javascript" type="text/javascript">         
    alert('Thank you for your message, we will be in contact shortly.');         
    window.location = 'contact.html';         
  </script>     
<?php } else { ?>     
  <script language="javascript" type="text/javascript">         
    alert('Message not sent. Please, notify the site administrator     info1@info.com');         
    window.location = 'contact.html';     
  </script>     
<?php     
}
} 
?>

【讨论】:

  • 嗯,这解决了我在测试时遇到的所有问题。
  • 好的,请将其标记为已解决,否则有人尝试再次解决此问题。 :)
  • 我不是问题所有者,但 $mail_to 仍未分配
  • 修复这个mail_to
  • 所以我复制了上面的代码,将 $radio=null: 更改为 $radio=depart(我假设这就是您所说的更改名称)经过测试,它没有发送,只显示一个空白白页....消息均未显示
猜你喜欢
  • 2020-05-14
  • 2019-05-12
  • 1970-01-01
  • 2020-03-27
  • 1970-01-01
  • 1970-01-01
  • 2012-06-19
  • 2013-06-13
  • 1970-01-01
相关资源
最近更新 更多