I am encountering a challenge with Vuejs as I work on a To-Do list. The tasks need to be stored in a MySQL database, and I also want the ability to delete tasks. While I have succeeded in importing and creating data, I'm facing difficulty in deleting data. I've spent a considerable amount of time trying to resolve this issue without success. Can someone provide me with assistance?
APP.js
var Todo = new Vue({
el: ".container",
data() {
return {
tasks: [],
taskText: "",
errorMessage: "",
successMessage: "",
notice: "",
selectTask: { tasko: "", taskcontent: "" },
}
},
computed: {
validateLengthLetter(){
if(this.selectTask.tasko.length>65)
return "Is too long"
}
},
validateLengthLetterButton(){
if(this.selectTask.tasko.length>65)
this.$refs.taskan.style.color="red"
},
mounted () {
this.readTasks();
},
methods: {
completeTask(event){
event.target.classList.toggle("completed");
},
readTasks() {
axios
.get("http://localhost/WhattoDOliste_VUE/db.php?action=read")
.then((data) => {
console.log(data.data);
this.tasks = data.data.tasken;
});
},
createTask() {
let formData = this.convertToFormData(this.selectTask);
axios
.post(
"http://localhost/WhattoDOliste_VUE/db.php?action=create",
formData
)
.then((data) => {});
//Clearing V-model
this.selectTask.tasko=""
this.selectTask.taskcontent="";
setTimeout(this.readTaskss,3)
},
deleteTask(id) {
let formData = this.convertToFormData(this.id);
axios
.post(
"http://localhost/WhattoDOliste_VUE/db.php?action=delete",
formData
)
.then(function (response) {
this.selectTask = { tasko: "", taskcontent: "" };
});
},
convertToFormData(data) {
let fd = new FormData();
for (value in data) {
fd.append(value, data[value]);
}
return fd;
},
},
});
db.php
<?php
//source: https://www.php.net/manual/de/mysqli.construct.php
$conn=new mysqli("localhost", "root", "", "todoliste");
//source https://www.php.net/manual/de/mysqli.connect-error.php
if($conn->connect_error){
error_log('Connection error: ' . $conn->connect_error);
}
$result=array("error"=>false);
$action="";
if(isset($_GET["action"])) {
$action=$_GET["action"];
}
//Read Data
if($action =="read"){
$sql=$conn->query("SELECT * FROM tasks");
$tasks=array();
while($row=$sql->fetch_assoc()){
array_push($tasks,$row);
}
$result["tasken"]=$tasks;
}
//Create Task
if($action =="create"){
$tasko=$_POST['tasko'];
$taskcontent=$_POST['taskcontent'];
$sql=$conn->query("INSERT INTO tasks (tasko,taskcontent)VALUES('$tasko','$taskcontent')");
}
//Update Date
if($action =="update"){
$id=$_POST["id"];
$tasko=$_POST["tasko"];
$taskcontent=$_POST["taskcontent"];
$sql=$conn->query("UPADATE tasks SET tasko='$tasko',taskcontent='$taskcontent',statue='$statue' WHERE id='$id' ");
}
//Delete Task
if($action =="delete"){
$id=$_POST['id'];
$sql=$conn->query("DELETE FROM tasks WHERE id='$id'");
}
$conn->close();
echo(json_encode($result));
?>
index.html
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="style.css">
</head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<body>
<div style="text-align: center;"></div>
<section>
<div class="container">
<!-- LEFT LINKS يمين -->
<div class="left">
<a href="index.html">
<h2>To Do List</h2>
</a>
<div class="cover">
<div class="inputs" v-if="selectTask">
<form action="" method="post">
<input type="text" id="myInput" placeholder="add Task.." name="tasko" v-model="selectTask.tasko">
<textarea name="" id="note" cols="30" rows="5" placeholder="Note"
name="taskcontent" v-model="selectTask.taskcontent"></textarea>
</form>
<div class="btns">
<button @click.prevent="createTask()" ref="taskan" v-if="selectTask.tasko" > <i class="fa fa-plus"></i>Add Task</button>
</div>
</div>
<div v-if="!selectTask.tasko" id="Warning">Please enter a task</div>
<div v-if="validateLengthLetter" id="Warning">{{validateLengthLetter}}</div>
</div>
<!-- RIGHT RECHTS يمين -->
<div class="right">
<div class="back">
<ul id="myUL" v-for="task in tasks" :key="task.id" v-if="task">
<li @click="task.status =!task.status" :key="task.id"> <p> {{task.tasko}}</p>
<span class="trash" name="delete" @click="deleteTask(task.id)"> <i class="fa fa-trash"></i></span>
<p v-if="!task.status"> {{task.taskcontent}} </p>
</li>
</div>
</div>
</div>
</section>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script src="app.js"></script>
</body>
</html>