diff --git a/Backend/controller/getPost.php b/Backend/controller/getPost.php index f2cfc87..02080ff 100644 --- a/Backend/controller/getPost.php +++ b/Backend/controller/getPost.php @@ -5,7 +5,10 @@ use Backend\Model\Post; header('Content-Type: application/json;charset=UTF-8'); -$data = Post::all(); +if(isset($_POST['id'])) + $data = Post::find($_POST['id']); +else + $data = Post::all(); echo $data; diff --git a/Backend/controller/update.php b/Backend/controller/update.php index 70f4581..fbd9a48 100644 --- a/Backend/controller/update.php +++ b/Backend/controller/update.php @@ -4,14 +4,12 @@ require "../../bootstrap.php"; use Backend\Model\Post; -if(isset($_POST['update'])){ +if(isset($_POST['id'])){ $post = Post::query()->find($_POST['id']); $post->title = $_POST['upTitle']; $post->description = $_POST['upDescription']; - if(!$post->update()){ - echo "Error could not update"; - }else{ - header("location: ../../Frontend/admin.php"); + if($post->update()){ + return 1; } } diff --git a/Backend/controller/upload.php b/Backend/controller/upload.php index 0fcbd9d..ecd1241 100644 --- a/Backend/controller/upload.php +++ b/Backend/controller/upload.php @@ -13,9 +13,6 @@ if(isset($_POST['title'])){ $post->description = $_POST['description']; //Call the save function. - if(!$post->save()){ - return http_response_code(400); - }else{ - echo $post->id; - } + if($post->save()) + echo json_encode(array($post->id, $post->created_at)); } diff --git a/Frontend/Assets/Ajax/createPost.js b/Frontend/Assets/Ajax/createPost.js index c08f96f..e609d6b 100644 --- a/Frontend/Assets/Ajax/createPost.js +++ b/Frontend/Assets/Ajax/createPost.js @@ -7,16 +7,20 @@ function createPost(title, desc) { $.ajax({ url: '../Backend/controller/upload.php', type: 'POST', + dataType: 'json', data: data, success: function (data) { + var d = new Date(data[1]) $('#table_id').DataTable().row.add([ + d.getDate() + '/' + (d.getMonth()+1)+'-'+d.getFullYear(), title, - desc, - 'Edit', - 'Delete' + 'Edit', + 'Delete' ]).draw().node().id = '1234'; }, - error: function () { + error: function (data) { + console.log("error"); + console.log(data); console.log("error"); } }); diff --git a/Frontend/Assets/Ajax/editPost.js b/Frontend/Assets/Ajax/editPost.js index 28df8c0..0f62bde 100644 --- a/Frontend/Assets/Ajax/editPost.js +++ b/Frontend/Assets/Ajax/editPost.js @@ -1,21 +1,55 @@ -/*function deletePost(id) { - return $.ajax({ +function editPost(id) { + $.ajax({ type: 'POST', - url: '../Backend/controller/deletePost.php', - data: {'delete_id':id}, + url: '../Backend/controller/getPost.php', + data: { id: id }, success:function (data) { - Swal.fire( - 'Posten er slettet!', - '', - 'success' - ) + Swal.fire({ + title: 'Rediger Post', + html: '
', + showCancelButton: true, + confirmButtonColor: '#3085d6', + cancelButtonColor: '#d33', + confirmButtonText: 'Rediger', + focusConfirm: false, + preConfirm: () => { + const title = Swal.getPopup().querySelector('#title').value + const desc = Swal.getPopup().querySelector('#description').value + if (!title || !desc) { + Swal.showValidationMessage(`Skriv noget i begge felter!`) + } else { + data.title = title; + data.description = desc; + } + return {title: title, desc: desc} + } + }).then((result) => { + $.ajax({ + type: 'POST', + url: '../Backend/controller/update.php', + data: {'id':id, 'upTitle': data.title, 'upDescription': data.description}, + success:function () { + Swal.fire( + 'Posten er redigeret!', + '', + 'success' + ).then(function() { + document.getElementById(id).children[1].innerHTML = data.title; + }) + }, + error:function (data) { + Swal.fire( + 'Posten kunne ikke blive redigeret', + '', + 'error' + ) + } + }); + }) }, error:function (data) { - Swal.fire( - 'Posten kunne ikke blive slettet', - '', - 'error' - ) + console.log(data); } }); -}*/ \ No newline at end of file + /**/ +} \ No newline at end of file diff --git a/Frontend/Assets/Ajax/fetchAndDisplayData.js b/Frontend/Assets/Ajax/fetchAndDisplayData.js index 965ddce..cb48d78 100644 --- a/Frontend/Assets/Ajax/fetchAndDisplayData.js +++ b/Frontend/Assets/Ajax/fetchAndDisplayData.js @@ -23,7 +23,7 @@ $(document).ready(function() { table.row.add([ d.getDate() + '/' + (d.getMonth()+1)+'-'+d.getFullYear(), data[i].title, - 'Edit', + 'Edit', 'Delete' ]).draw().node().id = data[i].id; } diff --git a/Frontend/Assets/Ajax/update.js b/Frontend/Assets/Ajax/update.js deleted file mode 100644 index 21f0b8d..0000000 --- a/Frontend/Assets/Ajax/update.js +++ /dev/null @@ -1,13 +0,0 @@ -function updatePost(id) { - $.ajax({ - type: 'POST', - url: '../Backend/controller/update.php', - data: {'update_id':id}, - success:function () { - console.log("Update was a success"); - }, - error:function (data) { - console.log("Update was not a success"); - } - }); -} \ No newline at end of file diff --git a/Frontend/edit.php b/Frontend/edit.php deleted file mode 100644 index 16a777b..0000000 --- a/Frontend/edit.php +++ /dev/null @@ -1,27 +0,0 @@ -find($id); -?> - - - - - Home - - - - -
- - - - -
- - - - -