dropbox1111 2015-07-07 15:35
浏览 40
已采纳

在使用PHP邮寄表单时使用if else if语句?

I have a form where I only want certain information to be emailed to an account depending on their selection from a select tag in the form.

Here is my HTML of the select tag:

<select id="numberofstaff">
    <option class="staffselection" value="">--Select--</option>
    <option class="staffselection" value="smalljobsite">1-3 staff</option>
    <option class="staffselection" value="mediumjobsite">4-7 staff</option>
    <option class="staffselection" value="largejobsite">8+ staff</option>
</select>

Here is my PHP that I'm having the issue with:

<?php
if (isset($_POST['primaryemail']) && isset($_POST['numberofstaff'])){
    $numberofstaff = $_POST['numberofstaff'];
    $smallplantable = $_POST['smallplantable'];
    $smallcopierone = $_POST['smallcopierone'];
    $smallcopiertwo = $_POST['smallcopiertwo'];
    $mediumtrailers = $_POST['mediumtrailers'];
    $mediumplantable = $_POST['mediumplantable'];
    $wifi = $_POST['wifi'];
    $mediumcopierone = $_POST['mediumcopierone'];
    $mediumcopiertwo = $_POST['mediumcopiertwo'];
    $largetrailers = $_POST['largetrailers'];
    $largeplantable = $_POST['largeplantable'];
    $largewifi = $_POST['largewifi'];
    $largecopierone = $_POST['largecopierone'];
    $largecopiertwo = $_POST['largecopiertwo'];
    $numberofstaff = $_POST['numberofstaff'];
    $smallplantable= $_POST['smallplantable'];
    $smallcopierone= $_POST['smallcopierone'];
    $smallcopiertwo= $_POST['smallcopiertwo'];
    $mediumtrailers= $_POST['mediumtrailers'];
    $mediumplantable= $_POST['mediumplantable'];
    $wifi= $_POST['wifi'];
    $mediumcopierone= $_POST['mediumcopierone'];
    $mediumcopiertwo= $_POST['mediumcopiertwo'];
    $largetrailers= $_POST['largetrailers'];
    $largeplantable= $_POST['largeplantable'];
    $largewifi= $_POST['largewifi'];
    $largecopierone= $_POST['largecopierone'];
    $largecopiertwo= $_POST['largecoipertwo'];

if (!empty($primaryemail) && $numberofstaff == 'smalljobsite') {
    $to = 'testemail@test.com';
    $subject = 'Jobsite Form Submitted';
    $body = Number of Users: " . $numberofstaff . "
Plan Table: " . $smallplantable . "
C5035: " . $smallcopierone . "
C5045: " . $smallcopiertwo;
    $headers = 'From:' . $primaryemail;
    mail($to, $subject, $body, $headers);   
}
else if (!empty($primaryemail) && $numberofstaff == 'mediumjobsite') {
    $to = testemail@test.com';
    $subject = 'Jobsite Form Submitted';
    $body = "Number of Users: " . $numberofstaff . "
Trailer(s) or Similar Sized Office(s): " . $mediumtrailers . "
Plan Table(s): " . $mediumplantable . "
External Wifi: " . $wifi . "
C5035: " . $mediumcopierone . "
C5045: " . $mediumcopiertwo;
    $headers = 'From:' . $primaryemail;
    mail($to, $subject, $body, $headers);   
}
else if (!empty($primaryemail) && $numberofstaff == 'largejobsite') {
    $to = 'testemail@test.com';
    $subject = 'Jobsite Form Submitted';
    $body = "Number of Users: " . $numberofstaff . "
Trailer(s) or Similar Sized Office(s): " . $largetrailers . "
Plan Table(s): " . $largeplantable . "
External Wifi: " . $largewifi . "
C5035: " . $largecopierone . "
C5045: " . $smallcopiertwo;
    $headers = 'From:' . $primaryemail;
        mail($to, $subject, $body, $headers);   
    }
}

The code works fine with just the one if statement to check make sure primaryemail is not empty. However, when I added the else if statements to change what is sent based on what is selected in the numberofstaff id it fails to work.

Any help is appreciated. I don't see why this wouldn't be possible.

  • 写回答

2条回答 默认 最新

  • dsc56927 2015-07-07 15:43
    关注

    I changed your code around a bit, added a switch on $numberofstaff and moved the mail $to, $from and actual mail functions outside the if clauses.

    Also you had some quotes missing in some if statements.
    Have a look

    <?php
    if (isset($_POST['primaryemail']) && isset($_POST['numberofstaff'])){
        $numberofstaff = $_POST['numberofstaff'];
        $smallplantable = $_POST['smallplantable'];
        $smallcopierone = $_POST['smallcopierone'];
        $smallcopiertwo = $_POST['smallcopiertwo'];
        $mediumtrailers = $_POST['mediumtrailers'];
        $mediumplantable = $_POST['mediumplantable'];
        $wifi = $_POST['wifi'];
        $mediumcopierone = $_POST['mediumcopierone'];
        $mediumcopiertwo = $_POST['mediumcopiertwo'];
        $largetrailers = $_POST['largetrailers'];
        $largeplantable = $_POST['largeplantable'];
        $largewifi = $_POST['largewifi'];
        $largecopierone = $_POST['largecopierone'];
        $largecopiertwo = $_POST['largecopiertwo'];
        $numberofstaff = $_POST['numberofstaff'];
        $smallplantable= $_POST['smallplantable'];
        $smallcopierone= $_POST['smallcopierone'];
        $smallcopiertwo= $_POST['smallcopiertwo'];
        $mediumtrailers= $_POST['mediumtrailers'];
        $mediumplantable= $_POST['mediumplantable'];
        $wifi= $_POST['wifi'];
        $mediumcopierone= $_POST['mediumcopierone'];
        $mediumcopiertwo= $_POST['mediumcopiertwo'];
        $largetrailers= $_POST['largetrailers'];
        $largeplantable= $_POST['largeplantable'];
        $largewifi= $_POST['largewifi'];
        $largecopierone= $_POST['largecopierone'];
        $largecopiertwo= $_POST['largecoipertwo'];
    
    if (!empty($primaryemail)){
        $to = 'testemail@test.com'; // moved here since they don't seem to depend on $number of staff
        $subject = 'Jobsite Form Submitted'; // moved here since they don't seem to depend on $number of staff
        $headers = 'From:' . $primaryemail;
    
        switch($numberofstaff){
            case 'smalljobsite':
                $body = "Number of Users: " . $numberofstaff . "
    Plan Table: " . $smallplantable . "
    C5035: " . $smallcopierone . "
    C5045: " . $smallcopiertwo;
            break;
            case 'mediumjobsite':
                $body = "Number of Users: " . $numberofstaff . "
    Trailer(s) or Similar Sized Office(s): " . $mediumtrailers . "
    Plan Table(s): " . $mediumplantable . "
    External Wifi: " . $wifi . "
    C5035: " . $mediumcopierone . "
    C5045: " . $mediumcopiertwo;
            break;
            case 'largejobsite':
                $body = "Number of Users: " . $numberofstaff . "
    Trailer(s) or Similar Sized Office(s): " . $largetrailers . "
    Plan Table(s): " . $largeplantable . "
    External Wifi: " . $largewifi . "
    C5035: " . $largecopierone . "
    C5045: " . $smallcopiertwo;
            break;
        }
    
        mail($to, $subject, $body, $headers);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助