只能检查1个复选框? PHP

时间:2016-05-15 14:00:49

标签: php checkbox

我遇到了复选框问题。我可以选中这两个复选框,但在提交后,只会选中一个复选框。我希望他们在提交后都进行检查。

提交后为什么不检查它们?这个过程出了什么问题?

<?php
$id = $_GET["id"];
$stmt = $dbConnection->prepare('SELECT * FROM paginas WHERE id = ?');
$stmt->bind_param('s', $id);
$stmt->execute();
$result = $stmt->get_result();
if(mysqli_num_rows($result) > 0) {
    while ($row = $result->fetch_assoc()) {
        ?>
        <a href="javascript:;" data-toggle="modal" data-target="#terugnaarpaginas" class="h1url">Terugkeren naar mijn pagina's</a>
        <h1>Wijzig pagina: <?php echo $row["name"]; ?></h1>
        <?php
        if(isset($_POST["opslaan"])) {
            if(empty($_POST["heading"])) {
                echo '<p class="error">Titel kan niet leeg zijn</p>';
            } elseif(empty($_POST["content"])) {
                echo '<p class="error">Content kan niet leeg zijn</p>';
            } else {
                $heading = $_POST["heading"];
                $content = $_POST["content"];
                $updated = date("d-m-Y H:i:s");
                $id = $row["id"];
                $public = $_POST["public"];
                $menu = $_POST["menu"];
                $stmt = $dbConnection->prepare('UPDATE paginas SET heading = ?, content = ?, updated = ?, public = ?, menu = ? WHERE id = ?');
                $stmt->bind_param('ssssss', $heading, $content, $updated, $public, $menu, $id);
                $stmt->execute();

                echo '<p class="success">Wijzigingen zijn succesvol opgeslagen. <a href="">Bekijken</a></p>';
            }
        } else {
?>
<form method="POST" action="">
    <input type="text" name="heading" id="fulltext" placeholder="Titel" value="<?php echo $row["heading"]; ?>">
    <textarea id="fullbox" class="editor" name="content"><?php echo $row["content"]; ?></textarea>
    <div class="pad"><input type="checkbox" name="public" id="public" value="<?php
    if($row["public"] == "1") {
        echo '0';
    } else {
        echo '1';
    }
    ?>" <?php
    if($row["public"] == "1") {
        echo ' checked';
    } else {
        echo '';
    }
    ?>><label for="public" class="checker">Gepubliceerd</label><input type="checkbox" name="menu" id="menu" value="<?php
    if($row["menu"] == "1") {
        echo '0';
    } else {
        echo '1';
    }
    ?>" <?php
    if($row["menu"] == "1") {
        echo ' checked';
    } else {
        echo '';
    }
    ?>><label for="menu" class="checker">Menu</label></div>
    <div class="clear"></div>
    <p id="left">Laatst gewijzigd: <?php echo $row["updated"]; ?></p><input type="submit" value="Bewaar wijzigingen" name="opslaan" class="nomp">
    <div class="clear"></div>
</form>
<?php
        }
    }
} else {
    echo '<p>Deze pagina bestaat niet.</p>';
}
?>

3 个答案:

答案 0 :(得分:0)

复选框的value应始终为1,而不是您在那里的条件,并且在准备将其放入数据库时​​,您应该这样做:

$public = isset($_POST['public']) ? 1 : 0;

否则,您将每隔一段时间提交一次0值,即使您选中此框也会关闭该值。

答案 1 :(得分:0)

除了Niet the Dark Absol上面的答案,你应该为每个元素创建动态名称,或使用name="public[]来捕获所有选定的值。

答案 2 :(得分:0)

您必须在名称后面设置[]的输入名称。比如check_list[]

例如:

<form action="test.php" method="post">
<input type="checkbox" name="check_list[]" value="value 1">
<input type="checkbox" name="check_list[]" value="value 2">
<input type="checkbox" name="check_list[]" value="value 3">
<input type="checkbox" name="check_list[]" value="value 4">
<input type="checkbox" name="check_list[]" value="value 5">
<input type="submit" />
</form>
<?php
if(!empty($_POST['check_list'])) {
    foreach($_POST['check_list'] as $check) {
            echo $check; //echoes the value set in the HTML form for each checked checkbox.
                         //so, if I were to check 1, 3, and 5 it would echo value 1, value 3, value 5.
                         //in your case, it would echo whatever $row['Report ID'] is equivalent to.
    }
}
?>