PHP Form选择获取所有值

时间:2019-01-03 11:46:33

标签: php arrays forms checkbox

有人能帮我理解我在var_dump产品ID时得到的是最后一个ID的数组,而不是全部都单独得到的吗?它会循环遍历所有选定的内容,因此应该为它们中的每一个返回ID值吗?

<?php
if(isset($_POST['feature'])){
    if(isset($_POST['test'])){
        foreach($_POST['test'] as $selected){
            $option = 'No';
            var_dump($row['product_id']);
        }
    }
}
?>

<form action="" method="POST">
    <?php
    $query = $products->findAll();
    foreach($query as $row){
        if($row['featured'] == 'Yes'){
            echo "<label for='featured'>" . $row['p_name'] . " </label> <input type='checkbox' name='test[]' value ='Yes'checked>
            <input type='hidden' name='product_id' value='" . $row['product_id'] . "'>";
        }
        else
        {
            echo "<label for='featured'>" . $row['p_name'] . " </label> <input type='checkbox' name='test[]' value ='No'>
            <input type='hidden' name='product_id' value='" . $row['product_id'] . "'>";
        }
    }
    ?>
    <input type="submit" name="feature" value="Feature">
</form>

1 个答案:

答案 0 :(得分:1)

不要在foreach内部进行var_dump,而应在foreach循环之外进行尝试!

 <?php
    if(isset($_POST['feature'])){
      if(isset($_POST['test'])){
        foreach($_POST['test'] as $selected){
        $option = 'No';
        $res = $selected['product_id'];
        }
        }
        var_dump($res);
      }
     ?>