-1

我有一些代码在提交后会显示来自我的电子邮件中的复选框的输入信息。这工作得很好,但是当用户没有从复选框中进行选择时,我希望它在我的电子邮件中显示“未选中任何选项”。我怎样才能做到这一点?下面是相关代码 - 我需要为这两个复选框实现这一点。我假设我需要某种 ELSE 语句。

代码:

if(!empty($_POST['features'])) {
foreach($_POST['features'] as $value) {
    $check_msg1 .= "Features checked: ".$value."\n";          
}}

if(!empty($_POST['fright'])) {
foreach($_POST['fright'] as $value) {
    $check_msg2 .= "Fright checked: ".$value."\n";            
}}
4

3 回答 3

1

Checkbox values are not submitted once not checked, so if $_POST['cb_name'] is empty, than nothing checked. According to this, you will need code like this(same for 'fright') :

if(!empty($_POST['features'])) {
   foreach($_POST['features'] as $value) {
       $check_msg1 .= "Features checked: ".$value."\n";          
   }
} else {
   $check_msg1 .= "No options checked\n"
}
于 2012-12-13T10:24:41.390 回答
1

如果用户没有检查任何东西,则不会有 $_POST['features'] 或 $_POST['fright']

你需要做一个 isset,或者更准确地说是一个!伊塞特

if ( ! isset($_POST['features']))
{
    $check_msg1 .= "No features selected.\n";
}
else
{
    foreach ($_POST['features'] as $value)
    {
        $check_msg1 .= "Features checked: " . $value . "\n";
    }
}

if ( ! isset($_POST['fright']))
{
    $check_msg2 .= "No fright selected.\n";
}
else
{
    foreach ($_POST['fright'] as $value)
    {
        $check_msg2 .= "Fright checked: " . $value . "\n";
    }
}
于 2012-12-13T10:24:48.377 回答
1

尝试:

if(isset($_POST['features'])) {
foreach($_POST['features'] as $value) {
    $check_msg1 .= "Features checked: ".$value."\n";          
}}
else {
    $check_msg1 .= "No options checked \n";
}

if(isset($_POST['fright'])) {
foreach($_POST['fright'] as $value) {
    $check_msg2 .= "Fright checked: ".$value."\n";            
}}
else {
    $check_msg2 .= "No options checked \n";
}

如果未选中任何选项,则 POST 不会为复选框返回任何值。因此,您需要检查 POST 数组中的值是否已设置。即使选中了复选框,检查“空”也会返回 true,但它的值是空的。

于 2012-12-13T10:25:22.467 回答