Skip to content

Commit

Permalink
#2 Create機能追加.
Browse files Browse the repository at this point in the history
  • Loading branch information
YutaroYutaro committed Dec 11, 2018
1 parent fa04838 commit b858a52
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 5 deletions.
20 changes: 18 additions & 2 deletions app/js/function.js
@@ -1,13 +1,29 @@
$('#createButton').on('click', function () {
let fd = new FormData($('#createForm').get(0));

$.ajax({
type: 'POST',
url: './app/php/create.php',
data: fd,
processData: false,
contentType: false,
})
.done((data) => {
console.log(data);
})
.fail(() => {
console.log('create fail...');
})
});

$('.updateButton').on('click', function () {
let updateId = $(this).parent().attr("id");
let title = $('#' + updateId + ' .card-title').html();
let comment = $('#' + updateId + ' .card-comment').html();
$('#update-title').val(title);
$('#update-comment').val(comment);
// console.log(updateId);

$('#modal-update-button').on('click', function () {
// console.log(updateId);
$('#modal-update-button').off('click');
})
});
Expand Down
25 changes: 25 additions & 0 deletions app/php/Class/Crud.php
Expand Up @@ -17,7 +17,29 @@ public function __construct()

public function Create($title, $comment)
{
$res = 0;
$error = 'create query fail...';

try {
$sql = 'INSERT INTO `bbs` (`title`, `comment`, `created_at`) VALUES (?, ?, ?)';

$now = new DateTime('now');

$createdAt = $now->format('Y-m-d H:i:s');

$data = [$title, $comment, $createdAt];

$stmt = $this->dbh->prepare($sql);

$stmt->execute($data);

$res = $stmt->rowCount();

} catch (PDOException $e) {
$error = $e->getMessage();
}

return empty($res) ? $error : $res;
}

public function Read()
Expand All @@ -44,6 +66,9 @@ public function Update($id, $title, $comment)

public function Delete($id)
{
$res = 0;
$error = 'delete query fail...';

try {
$sql = 'DELETE FROM `bbs` WHERE `id` = :id';

Expand Down
21 changes: 21 additions & 0 deletions app/php/create.php
@@ -0,0 +1,21 @@
<?php
/**
* Created by PhpStorm.
* User: nishikawa.yutaro
* Date: 2018-12-11
* Time: 14:32
*/

include __DIR__ . '/Class/Crud.php';

header('Content-type: text/plain; charset= UTF-8');

if (isset($_POST['title']) && isset($_POST['comment'])) {

$crud = new Crud();
$result = $crud->create($_POST['title'], $_POST['comment']);

echo json_encode($result);
} else {
echo "Fail to ajax request";
}
6 changes: 3 additions & 3 deletions index.php
Expand Up @@ -29,12 +29,12 @@

<!-- 入力フォーム -->
<div class="col-4">
<form action="" method="post">
<form id="createForm" action="#" method="post">
<label>タイトル</label>
<input type="text" class="form-control" id="title" name="title">
<input type="text" class="form-control" id="title" name="title" placeholder="タイトル">
<label>内容</label>
<textarea class="form-control" id="comment" name="comment" placeholder="内容"></textarea>
<button type="submit" class="btn btn-info">投稿する</button>
<button id="createButton" type="button" class="btn btn-info">投稿する</button>
</form>
</div>

Expand Down

0 comments on commit b858a52

Please sign in to comment.