使用foreach循环提交后保持复选框?

时间:2017-07-20 16:19:49

标签: javascript php ajax html5

我的代码是:

<form method="POST" action="" style="margin-top: 30px;">
<div class="form-group" style="display: inline-block;">
    <label for="Name" class="label-control" >Device Price</label>
    <input type="text" name="price" class="form-control">
</div>

<div class="row" style="margin-bottom: 20px;"> 
    <div class="col-md-3">      
        <?php
        $r = 0;
        $sql = "SELECT * FROM Subscribes ";
        $result = $db->query($sql);
        while ($row = $result->fetch_assoc()) {
            if ($r == 5) {
                echo "</div><div class='col-md-3'>";
                $r = 0;
            }
            $r++;
            $p = $row['price'];

            echo "<label style='font-size: 16px; margin: 20px;'>";
            echo "<input name='checkbox[]' type='checkbox' ";
            echo "value=" . $p . " >" . $row['sub_name'];

            if ($p == 0) {
                echo "<label style='color:#f00'> Free </label>";
            }
            echo "</label>";
        }
        ?>

    </div>            
</div> 

<div class="row">       
    <div class="col-md-12">
        <div class="form-group" style="margin-left: 35%;">
            <button  class="btn btn-info" name="sum" style="font-size: 24px;">TOTAL</button>
            <label style="margin-left: 6%; font-size: 24px;">
                <?php
                if (isset($_POST['sum'])) {
                    $price = $_POST['price'];
                    $price = $price + $price * .52;
                    $total = $price;
                    if (!empty($_POST['checkbox'])) {

                        foreach ($_POST['checkbox'] as $value) {
                            $total = $total + $value;
                        }
                    }
                    echo $total;
                }
                ?></label>
        </div>
    </div>
</div>

2 个答案:

答案 0 :(得分:0)

您可以将checked属性设置为复选框:<input type="checkbox" checked>"

在您的情况下,您还必须检查第一个块中的POST数据,并将checked属性添加到已提交的复选框中。

echo "<input name='checkbox[]' type='checkbox' ";
echo "value=" . $p;

//if value is in the array of submitted checkboxes, add the checked property
if(array_search($p ,$_POST["checkbox"]) >= 0) echo "checked"; 

echo ">" . $row['sub_name'];

答案 1 :(得分:0)

您几乎可以只检查发布的值以查看checked[$r]是否已启用,但如果用户未选中某些框,则不会发布这些框。

因此,您需要一种方法来识别每个复选框。您可以通过在回显时将它们命名为这样来完成此操作:

$id = $row['id']; // or whatever your id is
echo "<input name='checkbox[$id][]' type='checkbox' value=$p >"

现在,当用户提交表单时,您确切地知道应该检查哪些复选框。因此,您只需在呈现它们时添加checked属性:

echo "<input name='checkbox[$id][]' type='checkbox' value=$p $checked>"

总而言之:

<?php
    $r = 0;
    $sql = "SELECT * FROM Subscribes ";
    $result = $db->query($sql);

    while ($row = $result->fetch_assoc()) {
        if ($r == 5) {
            echo "</div><div class='col-md-3'>";
            $r = 0;
        }

        $r++;
        $p = $row['price'];
        $id = $row['id'];
        $checked = null;

        if(array_key_exists('checkbox', $_POST) && array_key_exists($id, $_POST['checkbox']){ 
            $checked = "checked";
        }

        echo "<label style='font-size: 16px; margin: 20px;'>";
        echo "<input name='checkbox[$id][]' type='checkbox' value=$p $checked>" . $row['sub_name'];

        if ($p == 0) {
            echo "<label style='color:#f00'> Free </label>";
        }
        echo "</label>";
    }
?>