From 4ce94621a7022ba7816bb5da8b0bdc688e27eee6 Mon Sep 17 00:00:00 2001 From: Victor Date: Fri, 4 Jun 2021 12:34:47 +0200 Subject: [PATCH] v0.0.8 Ajax now works with backend --- Backend/controller/upload.php | 24 ++++++++++----------- Backend/migrations/post_migration.php | 6 +++--- Frontend/Assets/Ajax/createPost.js | 15 ++++--------- Frontend/Assets/Ajax/fetchAndDisplayData.js | 4 +--- Frontend/home.php | 6 +++--- 5 files changed, 22 insertions(+), 33 deletions(-) diff --git a/Backend/controller/upload.php b/Backend/controller/upload.php index 0e5dd56..57a105a 100644 --- a/Backend/controller/upload.php +++ b/Backend/controller/upload.php @@ -4,19 +4,17 @@ require "../../bootstrap.php"; use Backend\Model\Post; -//Chek if the user did press the button on the home pages -if(isset($_POST['createPost'])){ - //Create a new initialize - $post = new Post(); +//Create a new initialize +$post = new Post(); - //Get the data from post and save it to the post model - $post->title = $_POST['title']; - $post->description = $_POST['description']; +//Get the data from post and save it to the post model +$post->title = $_POST['title']; +$post->description = $_POST['description']; - //Call the save function. - if(!$post->save()){ - return http_response_code(400); - }else{ - return http_response_code(200); - } +//Call the save function. +if(!$post->save()){ + return http_response_code(400); +}else{ + return http_response_code(200); } + diff --git a/Backend/migrations/post_migration.php b/Backend/migrations/post_migration.php index 7ff0dfc..8c6e988 100644 --- a/Backend/migrations/post_migration.php +++ b/Backend/migrations/post_migration.php @@ -5,7 +5,7 @@ use Illuminate\Database\Capsule\Manager as Capsule; Capsule::schema()->create('posts', function ($table) {//Note til mig selv, husk at din database table skal ende med "s" hvis du faa en lang fejl besked $table->increments('id'); - $table->string('title'); - $table->longtext('description'); + $table->string('title')->nullable(false); + $table->longtext('description')->nullable(false); $table->timestamps(); -}); +}); \ No newline at end of file diff --git a/Frontend/Assets/Ajax/createPost.js b/Frontend/Assets/Ajax/createPost.js index 118040b..3a799c5 100644 --- a/Frontend/Assets/Ajax/createPost.js +++ b/Frontend/Assets/Ajax/createPost.js @@ -1,22 +1,15 @@ $("#submit").click(function(){ - var $title = $("#title"); - var $description = $("#description"); - - var data = { - title: $title.val(), - description: $description.val(), + title: $("#title").val(), + description: $("#description").val(), }; - console.log(data); - $.ajax({ url: '../Backend/controller/upload.php', type: 'POST', data: data, - contentType: 'application/json; charset=utf-8', - success: function () { - console.log("Dansker bingo") + success: function (data) { + console.log(data) }, error: function () { console.log("error"); diff --git a/Frontend/Assets/Ajax/fetchAndDisplayData.js b/Frontend/Assets/Ajax/fetchAndDisplayData.js index c3877f4..243bc0a 100644 --- a/Frontend/Assets/Ajax/fetchAndDisplayData.js +++ b/Frontend/Assets/Ajax/fetchAndDisplayData.js @@ -13,6 +13,4 @@ $( document ).ready(function() { console.log(data); } }); -}); - - +}); \ No newline at end of file diff --git a/Frontend/home.php b/Frontend/home.php index 344e075..f1a3052 100644 --- a/Frontend/home.php +++ b/Frontend/home.php @@ -34,8 +34,8 @@ use Backend\Model\Post;

Opret post

- - + +
@@ -49,4 +49,4 @@ use Backend\Model\Post; - + \ No newline at end of file