PHP:检查多个复选框选项时回显HTML

时间:2015-11-22 05:48:57

标签: php checkbox

我已经编辑了这个问题,因为我觉得我需要在我的要求中更清楚。

以下代码与复选框中选中的选项有关。当两个值都被检查时它会回显“两个值”,并且它将回显“只有值A”,我无法让它回显“只有值B”,它反而打印出没有回声的空白。想法?

<?php
 if (( $a == "General" ) && ( $b == "Specialist" )) {
 echo '<h2>both values are printed in HTML</h2>';                                
 }
?>

<?php
 if (( $a == "General" ) && ( $b != "Specialist" )){
 echo '<h2>only valueA is printed in HTML</h2>';                                
 }
?>

<?php
 if (( $a != "General" ) && ( $b == "Specialist" )) {
 echo '<h2>only valueB is printed in HTML</h2>';                                
 }
?>

1 个答案:

答案 0 :(得分:0)

如果要检查值是否在数组中,则应使用in_array函数。您的代码应如下所示:

<?php

$CheckBox = $form_data['field'][1]

$a = ( "valueA");
$b = ( "valueB");

$isAChecked = in_array($a, $CheckBox[1]);
$isBChecked = in_array($b, $CheckBox[1]);
?>

<?php
if  ( $isAChecked && $isBChecked ) {
     echo '<h2>both values are printed in HTML</h2>';                                
} else if ( $isAChecked ) {
     echo '<h2>only valueA is printed in HTML</h2>';                                
} else if  ( $isBChecked ) {
     echo '<h2>only valueB is printed in HTML</h2>';                                
} else {
    echo '<h2>nothing printed</h2>'
}
?>