Skip to content

Commit

Permalink
#2 create機能のポスト内容とエラー文を表示するように変更.
Browse files Browse the repository at this point in the history
  • Loading branch information
YutaroYutaro committed Dec 13, 2018
1 parent 335291a commit e6b5000
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 38 deletions.
44 changes: 39 additions & 5 deletions app/js/function.js
@@ -1,6 +1,7 @@
$('#createButton').on('click', function () {
$('#success-alert').hide().html('');
$('#update-error-alert').hide().html('');
$('#create-error-alert').hide().html('');

let fd = new FormData($('#createForm').get(0));

Expand All @@ -11,19 +12,51 @@ $('#createButton').on('click', function () {
processData: false,
contentType: false,
})
.done((data) => {
.done((res) => {
let data = JSON.parse(res);

if (data['err'].length === 0) {
$('#bbs-body').prepend(
'<div class="card mb-3">' +
'<div id="' + data['data']['id'] + '" class="card-body">\n' +
'<h5 class="card-title">' + data['data']['title'] + '</h5>\n' +
'<p class="card-text card-comment">' + data['data']['comment'] + '</p>\n' +
'<p class="card-text">\n' +
'<small class="text-muted">' + data['data']['created_at'] + '</small>\n' +
'</p>\n' +
'<button type="button" class="btn btn-success updateButton" data-toggle="modal"\n' +
'data-target="#updateModal">修正する\n' +
'</button>\n' +
'<button type="button" class="btn btn-danger deleteButton" data-toggle="modal"\n' +
'data-target="#deleteModal">削除する\n' +
'</button>\n' +
'</div>' +
'</div>'
);
} else {
let errorText = '';

Object.keys(data['err']).forEach(function (key) {
errorText += (data['err'][key] + '<br>');
});

$('#create-error-alert').html(errorText).show();
}

console.log(data);
})
.fail(() => {
$('#create-error-alert').html('通信に失敗しました.').show();
console.log('create fail...');
})
});

let updateId;

$('.updateButton').on('click', function () {
$(document).on('click', '.updateButton', function () {
$('#success-alert').hide().html('');
$('#update-error-alert').hide().html('');
$('#create-error-alert').hide().html('');

updateId = $(this).parent().attr("id");
let title = $('#' + updateId + ' .card-title').html();
Expand Down Expand Up @@ -55,9 +88,9 @@ $('#modal-update-button').on('click', function () {
let position = $('#success-alert').html('更新に成功しました.').show().offset().top;

$('html, body').animate({
scrollTop : position
scrollTop: position
}, {
queue : false
queue: false
});

} else {
Expand All @@ -81,9 +114,10 @@ $('#modal-update-button').on('click', function () {

let deleteId;

$('.deleteButton').on('click', function () {
$(document).on('click', '.deleteButton', function () {
$('#success-alert').hide().html('');
$('#update-error-alert').hide().html('');
$('#create-error-alert').hide().html('');

deleteId = $(this).parent().attr('id');
});
Expand Down
2 changes: 1 addition & 1 deletion app/php/Class/BbsValidation.php
Expand Up @@ -34,4 +34,4 @@ public function CommentValidation($comment)

return $errors;
}
}
}
18 changes: 9 additions & 9 deletions app/php/Class/Crud.php
Expand Up @@ -15,31 +15,31 @@ public function __construct()
parent::__construct();
}

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

$this->dbh->beginTransaction();

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();
$res = $this->dbh->lastInsertId('id');

$this->dbh->commit();

} catch (PDOException $e) {
$error = $e->getMessage();
error_log($e->getMessage());
$this->dbh->rollBack();
}

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

public function Read()
Expand Down
2 changes: 1 addition & 1 deletion app/php/Class/Validation.php
Expand Up @@ -11,4 +11,4 @@ class Validation
public function MaxSize($str, $size) {
return (mb_strlen($str, 'UTF-8') > $size) ? false : true;
}
}
}
37 changes: 33 additions & 4 deletions app/php/create.php
Expand Up @@ -7,15 +7,44 @@
*/

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

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

$response = [];

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

$validation = new BbsValidation();

$titleValidation = $validation->TitleValidation($_POST['title']);
$commentValidation = $validation->CommentValidation($_POST['comment']);

if (!empty($titleValidation)) $errors = $errors + $titleValidation;
if (!empty($commentValidation)) $errors = $errors + $commentValidation;

if (!empty($errors)) {
$response = ['err' => $errors, 'data' => []];

} else {
$crud = new Crud();

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

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

$id = $crud->create($_POST['title'], $_POST['comment'], $createdAt);

if ($id === 0) $errors['db_error'] = '新規作成に失敗しました.';

$data = ['id' => $id,'title' => $_POST['title'], 'comment' => $_POST['comment'], 'created_at' => $createdAt];

$response = ['err' => $errors, 'data' => $data];

}
} else {
echo "Fail to ajax request";
$response = ['err' => ['server_error' => '要素が足りません.'], 'data' => []];
}

echo json_encode($response);
40 changes: 22 additions & 18 deletions index.php
Expand Up @@ -42,23 +42,25 @@
<!-- 投稿一覧 -->
<div id="bbs-index" class="col-8">
<div id="success-alert" class="alert alert-primary" role="alert" style="display: none;"></div>
<?php foreach ($contents as $content) : ?>
<div class="card mb-3">
<div id="<?php echo $content['id']; ?>" class="card-body">
<h5 class="card-title"><?php echo $content['title']; ?></h5>
<p class="card-text card-comment"><?php echo $content['comment']; ?></p>
<p class="card-text">
<small class="text-muted"><?php echo $content['created_at']; ?></small>
</p>
<button type="button" class="btn btn-success updateButton" data-toggle="modal"
data-target="#updateModal">修正する
</button>
<button type="button" class="btn btn-danger deleteButton" data-toggle="modal"
data-target="#deleteModal">削除する
</button>
</div>
<div id="bbs-body">
<?php foreach ($contents as $content) : ?>
<div class="card mb-3">
<div id="<?php echo $content['id']; ?>" class="card-body">
<h5 class="card-title"><?php echo $content['title']; ?></h5>
<p class="card-text card-comment"><?php echo $content['comment']; ?></p>
<p class="card-text">
<small class="text-muted"><?php echo $content['created_at']; ?></small>
</p>
<button type="button" class="btn btn-success updateButton" data-toggle="modal"
data-target="#updateModal">修正する
</button>
<button type="button" class="btn btn-danger deleteButton" data-toggle="modal"
data-target="#deleteModal">削除する
</button>
</div>
</div>
<?php endforeach; ?>
</div>
<?php endforeach; ?>
</div>

<!-- Update Modal -->
Expand All @@ -73,7 +75,8 @@
</button>
</div>
<div class="modal-body">
<div id="update-error-alert" class="alert alert-danger" role="alert" style="display: none;"></div>
<div id="update-error-alert" class="alert alert-danger" role="alert"
style="display: none;"></div>
<form id="updateForm" action="" method="post">
<label>タイトル</label>
<input type="text" class="form-control" id="update-title" name="title">
Expand Down Expand Up @@ -101,7 +104,8 @@
</button>
</div>
<div class="modal-body">
<div id="delete-error-alert" class="alert alert-danger" role="alert" style="display: none;"></div>
<div id="delete-error-alert" class="alert alert-danger" role="alert"
style="display: none;"></div>
本当に削除しますか?
</div>
<div class="modal-footer">
Expand Down

0 comments on commit e6b5000

Please sign in to comment.