Only 1 checkbox can be checked? PHP

318

I'm having an issue with checkboxes. I can check both checkboxes, but after submitting, only 1 checkbox is checked. I want them to both be checked after submitting.

Why are they not both checked after submit? What's going wrong in the process?

<?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" >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 >Titel kan niet leeg zijn</p>';
      } elseif(empty($_POST["content"])) {
        echo '<p >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 >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" name="content"><?php echo $row["content"]; ?></textarea>
  <div ><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" >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" >Menu</label></div>
  <div ></div>
  <p id="left">Laatst gewijzigd: <?php echo $row["updated"]; ?></p><input type="submit" value="Bewaar wijzigingen" name="opslaan" >
  <div ></div>
</form>
<?php
    }
  }
} else {
  echo '<p>Deze pagina bestaat niet.</p>';
}
?>
27

Answer

Solution:

The{-code-1} of the checkbox should always be1 instead of the condition you have there, and when preparing to put it in the database you should do:

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

Otherwise, you will be submitting the {-code-1}0 every other time, which will turn the {-code-1} off even if you checked the box.

872

Answer

Solution:

In addition to Niet the Dark Absol's answer above you should create dynamic names for each element or usename="public[] to capture all selected values.

539

Answer

Solution:

You must set your input name with[] after the name. likecheck_list[].

For example:

<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.
    }
}
?>

People are also looking for solutions to the problem: php - Send deleted rows to another table

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.