$ _POST复选框数组仅返回单个值

时间:2016-09-09 06:58:18

标签: php html mysql post

$ _POST [' clients']结束时的foreach循环只返回一个值。怎么了?

 <?php $clients_to_display = Client::find_all(); ?>
            <p><?php foreach ($clients_to_display as $key) {
                echo $key->name; ?>:<input type='checkbox' name='clients[]' value=<?php $key->name; ?></><br/>
            <?php } ?></p>

        if(isset($_POST['submit'])){

        $job->name = $_POST["job_name"];
        $job->description = $_POST["job_description"];
        $job->type = $_POST["job_type"];
        $job->age = $_POST["job_age"];

    foreach ($_POST['clients'] as $key) {
        echo $key;
    }
}

1 个答案:

答案 0 :(得分:0)

此代码不执行任何操作:

<?php $key->name; ?>

我想你想要:

<?php echo($key->name); ?>

此外,它必须用引号括起来:

value="<?php echo($key->name); ?>"

此外,标记无效。所以,整行应该是:

echo($key->name); ?>:<input type="checkbox" name="clients[]" value="<?php echo($key->name); ?>" /><br/>