仅在PHP邮件中选择时显示下拉菜单内容

时间:2017-04-14 19:26:55

标签: php html email drop-down-menu ternary-operator

我创建了一个通过PHP Mail发送的HTML表单。一切都很好,但我想简化它,以便在发送的电子邮件中,只显示具有实际选择的下拉菜单。现在它的设置使得每个下拉菜单都显示在电子邮件中,即使用户选择了0。我已经有很多下拉菜单了,还有更多要添加的内容,以便电子邮件能够真正获得凌乱。

逻辑上,一个简单的if语句应该可以胜任,但我不确定如何做到这一点,因为我只有一行$email_body=""来发送内容到电子邮件,我不能在那里插入一个if / else语句。我查看了三元运算符,但我的PHP知识有限,我无法使其工作。谢谢!

HTML(简化以获得想法)

<form>    
  <div class="tab-content clearfix">
        <div class="tab-pane active" id="carpet-cleaning">
            <h3 class="options-text">Carpet Cleaning Options</h3>
                <!--how many bedrooms-->
                <label class="control-label" for="carpet_cleaning">Bedrooms</label><br>
                <select class="form-control-1" id="carpet_cleaning" name ="bedrooms_selection">
                    <option value="0">0</option>
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                </select>
                <!--how many dining rooms-->
                <br><label class="control-label" for="dining_rooms">Dining Rooms</label><br>
                <select class="form-control-1" id="dining_rooms" name="dining_selection">
                    <option value="0">0</option>
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                    <!--how many family rooms-->
                    <br><label class="control-label" for="family_rooms">Family Rooms</label><br>
                <select class="form-control-1" id="family_rooms" name="family_room_selection">
                    <option value="0">0</option>
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                </select>
  </div>
 </div>
</form>

PHP

<?php
$bedrooms = $_POST['bedrooms_selection'];
$diningRooms = $_POST['dining_selection'];
$familyRooms = $_POST['family_room_selection'];
$email_subject = "Price Quote";
$email_body="Carpet Services Needed: \n Bedrooms: $bedrooms \n Dining Rooms: $diningRooms \n Family Rooms: $familyRooms";
$to = "jon@example.com";
$headers = "From: $email";
mail($to, $email_subject, $email_body, $headers);
header("Location: http://example.com");
?>

因此,为了简化,如果用户在下拉菜单中选择0,我不希望它显示在正在发送的电子邮件中。只能用一个不允许if / else语句的字符串来解决这个问题。

1 个答案:

答案 0 :(得分:2)

您可以与if进行比较,然后构建email_body字符串

<x:String x:Key="Test">M10,16.5V7.5L16,12M12,2A10,10 0 0,0 2,12A10,10 0 0,0 12,22A10,10 0 0,0 22,12A10,10 0 0,0 12,2Z</x:String>