【问题标题】:Using if else if statement when mailing form using PHP?使用 PHP 邮寄表单时使用 if else if 语句?
【发布时间】:2015-07-07 15:35:55
【问题描述】:

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

这是我的选择标签的 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 中选择的内容更改发送的内容时,它无法工作。

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

【问题讨论】:

  • 是语法错误吗? $body = 用户数:" . $numberofstaff 应该是 $body = "用户数:" . $numberofstaff

标签: php html


【解决方案1】:
isset($_POST['numberofstaff'])

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

<select id="numberofstaff">

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

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

【讨论】:

    【解决方案2】:

    我稍微更改了您的代码,在$numberofstaff 上添加了switch,并将邮件$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);
    }
    

    【讨论】:

    • 这实际上比我试图做的更有意义。谢谢亚历克斯。标记为答案。
    猜你喜欢
    • 2013-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-09
    • 1970-01-01
    • 2015-07-07
    • 2020-08-20
    相关资源
    最近更新 更多