v0.0.6 Made edit and update now works dynamically

This commit is contained in:
Victor 2021-06-03 12:50:28 +02:00
parent 256f163771
commit 4024c0dd99
9 changed files with 94 additions and 30 deletions

View File

@ -1,17 +1,17 @@
<?php
require "accessControl.php";
require "../../bootstrap.php";
use Backend\Model\Post;
if(isset($_GET['id'])){
$id = $_GET['id'];
if(isset($_GET['delete_id'])){
$id = $_GET['delete_id'];
$findId = Post::query()->findOrFail($id);
if(!$findId->delete()){
echo "Error could not delete record";
return "Error could not delete";
}else{
header("location: ../../Frontend/home.php?recordDeleted");
return "Record deleted";
}
}

View File

@ -7,9 +7,4 @@ use Backend\Model\Post;
if(isset($_GET['id'])){
$id = $_GET['id'];
$post = Post::query()->find($id);
if($post){
echo $post;
}
}

View File

@ -8,12 +8,12 @@ if (!isset($_SESSION['token'])) {
header("location: ../../Frontend/index.php?login=notloggedin");
}
header('Content-Type: application/json;charset=UTF-8');
echo "<pre>";
$data = Post::all();
//HOme.php
$data = Post::all();
echo $data;

View File

@ -5,12 +5,9 @@ require "../../bootstrap.php";
use Backend\Model\Post;
if(isset($_POST['update'])){
$id = $_POST['id'];
$title = $_POST['upTitle'];
$description = $_POST['upDescription'];
$post = Post::query()->find($id)->first();
$post->title = $title;
$post->description = $description;
$post = Post::query()->find($_POST['id']);
$post->title = $_POST['upTitle'];
$post->description = $_POST['upDescription'];
if(!$post->update()){
echo "Error could not update";

View File

@ -0,0 +1,13 @@
function deletePost(id) {
$.ajax({
type: 'get',
url: '../Backend/controller/deletePost.php',
data: {'delete_id':id},
success:function (data) {
console.log(data);
},
error:function (data) {
console.log(data);
}
});
}

View File

@ -0,0 +1,18 @@
$( document ).ready(function() {
$.ajax({
type: 'POST',
url: '../Backend/controller/getPost.php',
success:function (data) {
console.log(data);
for (let i = 0; i < data.length; i++){
document.getElementById("dis").innerHTML += "<button onClick=\"deletePost(" + data[i].id + ")\">Slet opslag</button><br>";
document.getElementById("dis").innerHTML += "<a href=\"edit.php?id="+ data[i].id + "\">Edit</a> <br><hr>"
}
},
error:function (data) {
console.log(data);
}
});
});

View File

@ -9,12 +9,16 @@ require "../Backend/controller/accessControl.php";
<title>Home</title>
</head>
<body>
<?php $ad = $_GET['id']; ?>
<form action="../Backend/controller/update.php" method="POST">
<input type="number" name="id" hidden value="1">
<input type="number" name="id" hidden value="<?php echo $ad ?>">
<input type="text" name="upTitle" placeholder="Title">
<input type="text" name="upDescription" placeholder="Description">
<input type="submit" name="update">
</form>
</body>
</html>

View File

@ -1,13 +1,33 @@
<?php
require "../Backend/controller/accessControl.php";
require "../bootstrap.php";
use Backend\Model\Post;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<meta charset="UTF-8">
<title>Home</title>
</head>
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
</style>
<body>
<p>Logget in som <?php echo $_SESSION['name'];?></p>
<a href="../Backend/controller/logout.php">Log ud</a>
@ -19,15 +39,13 @@ require "../bootstrap.php";
<input type="submit" name="createPost" placeholder="Opret">
</form>
<hr>
<p>Slet post</p>
<?php
$posts = \Backend\Model\Post::all()->pluck('id');
foreach ($posts as $post){
echo "<a href='../Backend/controller/deletePost.php?id=1'>Delete</a> <br>";
}
?>
<hr>
<p>Edit post</p>
<a href='edit.php?id=1'>Edit 1</a>
<div id="dis"></div>
<script src="Assets/Ajax/deletePost.js"></script>
<script src="Assets/Ajax/fetchAndDisplayData.js"></script>
</body>
</html>

View File

@ -420,6 +420,25 @@
dynamicMainBullets: 3,
}
});
/*dasdasda*/
$( document ).ready(function() {
$.ajax({
type: 'POST',
url: '../Backend/controller/getPost.php',
success:function (data) {
console.log(data);
},
error:function (data) {
console.log(data);
}
});
});
</script>
</body>
</html>