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

时间:2015-07-07 15:35:56

标签: php html

我有一个表单,我只希望将某些信息通过电子邮件发送到帐户,具体取决于他们从表单中的选择标记中选择的内容。

以下是我的选择标记的HTML:

<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>

这是我的PHP,我遇到了问题:

<?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 . "\r\nPlan Table: " . $smallplantable . "\r\nC5035: " . $smallcopierone . "\r\nC5045: " . $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 . "\r\nTrailer(s) or Similar Sized Office(s): " . $mediumtrailers . "\r\nPlan Table(s): " . $mediumplantable . "\r\nExternal Wifi: " . $wifi . "\r\nC5035: " . $mediumcopierone . "\r\nC5045: " . $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 . "\r\nTrailer(s) or Similar Sized Office(s): " . $largetrailers . "\r\nPlan Table(s): " . $largeplantable . "\r\nExternal Wifi: " . $largewifi . "\r\nC5035: " . $largecopierone . "\r\nC5045: " . $smallcopiertwo;
    $headers = 'From:' . $primaryemail;
        mail($to, $subject, $body, $headers);   
    }
}

代码工作正常,只需要检查一个if语句,确保primaryemail不为空。但是,当我添加else if语句来根据numberofstaff id中选择的内容更改发送内容时,它无法工作。

感谢任何帮助。我不明白为什么这是不可能的。

2 个答案:

答案 0 :(得分:1)

isset($_POST['numberofstaff'])

这永远不会被设置,因为您没有该名称的表单元素。你有这个:

<select id="numberofstaff">

发布的表单值是name,而不是id

<select id="numberofstaff" name="numberofstaff">

答案 1 :(得分:0)

我稍稍更改了您的代码,在switch上添加了$numberofstaff,并将邮件$to$from和实际邮件功能移到了if之外条款。

在某些if语句中,你也有一些引号丢失 看看

<?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 . "\r\nPlan Table: " . $smallplantable . "\r\nC5035: " . $smallcopierone . "\r\nC5045: " . $smallcopiertwo;
        break;
        case 'mediumjobsite':
            $body = "Number of Users: " . $numberofstaff . "\r\nTrailer(s) or Similar Sized Office(s): " . $mediumtrailers . "\r\nPlan Table(s): " . $mediumplantable . "\r\nExternal Wifi: " . $wifi . "\r\nC5035: " . $mediumcopierone . "\r\nC5045: " . $mediumcopiertwo;
        break;
        case 'largejobsite':
            $body = "Number of Users: " . $numberofstaff . "\r\nTrailer(s) or Similar Sized Office(s): " . $largetrailers . "\r\nPlan Table(s): " . $largeplantable . "\r\nExternal Wifi: " . $largewifi . "\r\nC5035: " . $largecopierone . "\r\nC5045: " . $smallcopiertwo;
        break;
    }

    mail($to, $subject, $body, $headers);
}