2

I am currently building a todo-website. I can add todo's to the Dataase, and I can also delete them, but I can only delete once per click on the button. I tried to set the name of the li, as an array, and made a foreach, which I thought would delete every checked checkbox, but I can still delete only ONE entry per click on the button. Here is my full code:

<!DOCTYPE html>
<?php session_start(); ?>
<html lang="de">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Todo-App</title>
  <link rel="stylesheet" href="css/materialize.min.css">
  <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
  <link rel="stylesheet" href="css/eigenes.css">
</head>
<body>
<div class="container">
  <nav>
    <div class="nav-wrapper z-depth-1">
      <a href="index.php" class="brand-logo center">Todo <i class="material-icons right">add_circle_outline</i></a>
    </div>
  </nav>
<div class="row">
<h3>Todo-App</h3>
<div class="divider"></div>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<div class="col s12">
          <div class="input-field inline">
            <input name="todo" type="text" class="validate">
            <label for="todo">Todo</label>
               <input id="absenden" type ="submit" class ="btn waves-effect waves-light inline" value = "Hinzufügen"/>
          </div>
</div>
<ul id="Liste" class="collection z-depth-1">
  <li class="collection-header center"><h4>Todo-Liste</h4></li>
  <?php
  try {
    $db = new PDO("mysql:dbname=todo;host=localhost",
                        "root",
                        "");
  }catch (PDOException $e) {
    echo "Fehler: " . htmlspecialchars($e->getMessage());
    exit();
  }
    $stmt = $db->query("Select * from todo");
    while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
      echo "<li class='collection-item'>" . $row["text"] . "<label class='right'><input type='checkbox' name='entry[]'  value='". $row["id"]. "'> <span>Löschen</span></label> </li>" ;
    }
   ?>
</ul>
</form>
</div>
<script src="js/jquery-3.3.1.min.js" charset="utf-8"></script>
<script src="js/materialize.min.js" charset="utf-8"></script>

<script>
if ($("#Liste li").length > 1) {
$("#absenden").val("Hinzufügen/Löschen");
$("#absenden").addClass("blue lighten-3");
}

</script>
</body>
</html>
<?php
// $todo  = isset($_POST["todo"]) && is_string($_POST["todo"]) ? $_POST["todo"] : "";


$isSet  = isset($_POST["todo"]);
if($isSet == 1) {
  $todo = $_POST["todo"];
}else {}


if($isSet == 1 && $todo != "") {
$sql = "INSERT INTO todo(text) VALUES ('$todo');";

$db->exec($sql);
header("Refresh:0");
}elseif ($isSet == 1 && $todo == "") {
  if(isset($_POST['entry'])) {
    foreach($_POST["entry"] as $entry)
    $sql = "DELETE FROM todo WHERE id = " . $entry . ";";
    $db->exec($sql);
    header("Refresh:0");
    echo "Häkchen gesetzt";
  }
}

1 Answer 1

1

You should set your li with the checkbox in another form and add some submit at the end. I've done this this way :

<form action="#" method="POST">
  <ul id="Liste" class="collection z-depth-1">
    <li class="collection-header center"><h4>Todo-Liste</h4></li>
    <?php
      // $stmt = $db->query("Select * from todo");
      // while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
      //
      // }
      $rowId = [123, 456, 789, 321, 654];
      echo "<li class='collection-item'> some text <label class='right'><input type='checkbox' name='" . $rowId[0] . "'  value='123'> <span>Löschen</span></label></li>" ;
      echo "<li class='collection-item'> some text <label class='right'><input type='checkbox' name='" . $rowId[1] . "'  value='456'> <span>Löschen</span></label> </li>" ;
      echo "<li class='collection-item'> some text <label class='right'><input type='checkbox' name='" . $rowId[2] . "'  value='789'> <span>Löschen</span></label> </li>" ;
      echo "<li class='collection-item'> some text <label class='right'><input type='checkbox' name='" . $rowId[3] . "'  value='321'> <span>Löschen</span></label> </li>" ;
      echo "<li class='collection-item'> some text <label class='right'><input type='checkbox' name='" . $rowId[4] . "'  value='654'> <span>Löschen</span></label> </li>" ;
     ?>
  </ul>
  <input type="submit">
</form>
<?php
foreach ($rowId as $val) {
  if(isset($_POST[$val])) {
    echo "<p>FAKE SQL DELETE WHERE id= ". $val ."</p>";
  }
}
 ?>

I've no db connected so I've mocked some data, but you can use it to implement your sql queries as well.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.