0

我有 6 个美国州(夏威夷、俄亥俄、路易斯安那、弗吉尼亚、田纳西和德克萨斯)。我还有另外 3 个美国州的数组,它们是从上述 6 个美国州(俄亥俄州、弗吉尼亚州、德克萨斯州)中选出的。

如何让 PHP 生成总共 6 个状态的复选框列表,但选中的状态被选中?我的代码在下面列出,我似乎无法让它运行:

<!DOCTYPE> 
<html>
<head>
    <style>
    #locationsBox { 
        border: 2px solid #ccc; 
        width: 250px; 
        height: 100px; 
        overflow-y: scroll; 
    }
    </style>
</head>
<body>
    <div id="locationsBox">
    <form name="location_checkboxes">
    <?
    $locations = array("Hawaii","Ohio","Louisiana","Virginia","Tennessee","Texas");
    $selected_locations = array("Ohio","Virginia","Texas");

    for($i = 0; $i < sizeOf($locations); $i++) {
        if (false != $key = array_search($selected_locations[$i],$locations) {
            echo '<input type="checkbox" checked="yes" value="'.$locations[$key].'" />' . $locations[$key] . '<br />';
        } else {
            echo '<input type="checkbox" value="'.$locations[$i].'" />' . $locations[$i] . '<br />';
        }
    }
    ?>
    </form>
    </div>
</body>
</html>

感谢您的任何建议!

4

1 回答 1

1
<?php
$locations = array("Hawaii","Ohio","Louisiana","Virginia","Tennessee","Texas");
$selected_locations = array("Ohio","Virginia","Texas");
foreach($locations as $l)
{
    if(in_array($l,$selected_locations))
    {
       echo '<input type="checkbox" name="location[]"     checked="checked" value="'.$l.'" />' . $l . '<br />';
        echo "<br />";
    }
     else 
    {
        echo '<input type="checkbox" name="location[]" value="'.$l.'" />' . $l . '<br />';
    }
}
?>
于 2013-01-01T14:55:44.463 回答