【问题标题】:if radio box checked send different email fields如果选中单选框,则发送不同的电子邮件字段
【发布时间】:2011-10-29 18:38:04
【问题描述】:
<form method="post" action="" id="infoform">
<label for="first">
    First Name</label>
<input type="text" name="first" id="first" class="field validate[required]">
</br>
<label for="surname">
    Last Name</label>
<input type="text" name="surname" id="surname" class="field validate[required]"> </br>
    <label for="email">
        Email</label>
    <input type="text" name="email" id="email" class=" field email validate[required,custom[email]]">
        </br>
        <div class="radios">
            <input id="test2" checked="checked" type="radio" name="message" value="register" />
        Register for Updates</input>
    <input id="test1" type="radio" name="message" value="msgs" />
    Send a Message</input>
</div>
<div class="msg">
    <label for="message" id="messagelabel">
        Message</label>
    <textarea type="text" name="message" id="message"></textarea>
</div>
<input id="submit" type="submit" value=""><div class="test3">
    <a class="sbmt">Register</a></div>
    <div class="test4">
        <a class="sbmt">Send</a></div>
</input>
</form>

如果选中单选框“test1”,则会出现 div msg。然后通过 ajax 将其发送到电子邮件 php 脚本:

<?php
    $EmailTo = "matt@explosivetitles.com";
    $adminSubject = "Civitas Message";

    $firstName = Trim(stripslashes($_POST['first']));
    $Surname = Trim(stripslashes($_POST['surname'])); 
    $Email = Trim(stripslashes($_POST['email'])); 
    $Message = Trim(stripslashes($_POST['message'])); 

    $adminheaders = 'From: "Civitas website" <info@civitas.com>';

    $adminBody .= "First Name: ";
    $adminBody .= $firstName;
    $adminBody .= "\n";
    $adminBody .= "Surname: ";
    $adminBody .= $Surname;
    $adminBody .= "\n";
    $adminBody .= "Email: ";
    $adminBody .= $Email;
    $adminBody .= "\n";
    $adminBody .= "Message: ";
    $adminBody .= $Message;
    $adminBody .= "\n";


    $adminMail = mail($EmailTo, $adminSubject, $adminBody, $adminheaders);


    if ($adminMail){
        echo "true";
    }
    else{
      echo "false";
    }
?>

但是,如果显示消息框,我只希望发送电子邮件的“消息”部分。我该怎么做?

【问题讨论】:

  • 您希望“消息”部分显示在哪里?你的问题不太清楚,你应该编辑它以获得答案

标签: php email radio-button


【解决方案1】:

你可以这样做:

$adminBody .= "First Name: ";
$adminBody .= $firstName;
$adminBody .= "\n";
$adminBody .= "Surname: ";
$adminBody .= $Surname;
$adminBody .= "\n";
$adminBody .= "Email: ";
$adminBody .= $Email;
$adminBody .= "\n";
if (isset($_POST['message'])) {
  $adminBody .= "Message: ";
  $adminBody .= $Message;
}
$adminBody .= "\n";

isset() 会检查变量是否存在,如果存在则返回true

【讨论】:

    【解决方案2】:

    PHP 不查看元素的id,而是在发送POSTGET 方法时查看name 属性。在您编写的代码中,您有两个输入元素 textarearadio input 具有相同的名称“消息”。当两者都发送时,$_POST 变量变成一个数组,并使用name 属性作为提交值的索引,PHP 数组不能有重复的索引,因此为了解决这个问题,你重命名一个。对于您的radio input,我建议将name 属性设置为type

    <?php
        $send           = "matt@explosivetitles.com";
        $subject        = "Civitas Message";
        $details        = array (
            "first"     => @$_POST["first"],
            "surname"   => @$_POST["surname"],
            "email"     => @$_POST["email"],
            "message"   => @$_POST["message"]
        );
        foreach ($details as $index => $value) {
            $details[$index] = trim ( stripslashes ($value));
        }
        if (@$_POST["type"] == "register") {
            // Whatever happens when they select "register"
        } else if (@$_POST["type"] == "msgs") {
            $header     = "From 'Civitas website' <info@civitas.com>";
            $body       = $details["first"] . " " . $details["surname"] . " "
                        . "<" . $details["email"] . ">"
                        . "Message: \n"
                        . $details["message"];
    
            $mail       = mail ($send, $subject, $body, $header);
            if ($mail) {
                return true;
            } else {
                return false;
            }
        }
    ?>
    

    另外,你的代码看得我眼花,所以我重写了。

    【讨论】:

      猜你喜欢
      • 2015-08-10
      • 2011-01-14
      • 2013-09-30
      • 2014-02-23
      • 2013-04-17
      • 2017-05-29
      • 1970-01-01
      • 2018-09-09
      • 2011-08-09
      相关资源
      最近更新 更多