1

我在提交时设置了复选框,我想继续勾选我选择的内容,我已经写了一个代码,但我想知道任何最简单的方法来做到这一点

<form action="" method="post">


                    <input type="checkbox" name="car[]"  
                        <?php 
                            if(isset($_POST['submit'])){ 
                                if(isset($_POST['car'])){
                                if(in_array('Bus',$_POST['car']))
                                    { 
                                        echo  ' checked="checked"" '; 
                                    }
                                }
                            } 
                        ?> value="Bus" />Bus
        <input type="checkbox" name="car[]" value="Van" 
                        <?php 
                            if(isset($_POST['submit']))
                            { 
                                if(isset($_POST['car'])){
                                    if(in_array('Van',$_POST['car']))
                                    { 
                                        echo  ' checked="checked"" '; 
                                    }
                                }
                            } ?> />Van

            <input type="submit" name="submit" value="Submit" />

</form>
4

1 回答 1

0

您可以一举遍历车辆,并相应地打印出项目(及其相应的检查值)。像这样的东西会起作用:

$vehicles = array('Bus', 'Van'); // You can add as many as you want!

foreach($vehicles as $vehicle) {
    // Should it be checked?
    $checked = '';
    if(isset($_POST['car'])) {
        if(in_array($vehicle,$_POST['car']) $checked="checked='checked'";
    }

    // Add the element
    echo "<input type='checkbox' name='car[]' value='{$vehicle}' {$checked}> 
        {$vehicle}";
}
于 2013-08-31T05:30:36.757 回答