mirror of
https://github.com/sergeyyarkov/blog-expressjs.git
synced 2026-10-11 02:09:00 +03:00
Added check number in field id
This commit is contained in:
parent
5828ed1e20
commit
184c03aeaf
11
index.js
11
index.js
@ -78,6 +78,17 @@ app.post('/update', (req, res) => {
|
|||||||
date = req.body.date,
|
date = req.body.date,
|
||||||
description = req.body.description;
|
description = req.body.description;
|
||||||
|
|
||||||
|
const data = {
|
||||||
|
id: req.body.id,
|
||||||
|
title: req.body.title,
|
||||||
|
date: req.body.date,
|
||||||
|
description: req.body.description
|
||||||
|
};
|
||||||
|
|
||||||
|
for (let k in data) {
|
||||||
|
console.log(k + ' : ' + data[k]);
|
||||||
|
}
|
||||||
|
|
||||||
res.send(`<p>ID: ${id}<br>Название: ${title}<br>Дата: ${date}<br>Описание: ${description}</p>`);
|
res.send(`<p>ID: ${id}<br>Название: ${title}<br>Дата: ${date}<br>Описание: ${description}</p>`);
|
||||||
});
|
});
|
||||||
// UPDATE `blog`.`posts` SET `description` = 'gfhj' WHERE (`id` = '10');
|
// UPDATE `blog`.`posts` SET `description` = 'gfhj' WHERE (`id` = '10');
|
||||||
|
|||||||
@ -12,6 +12,23 @@ textarea {
|
|||||||
resize: none;
|
resize: none;
|
||||||
margin-top: 5px;
|
margin-top: 5px;
|
||||||
}
|
}
|
||||||
|
label {
|
||||||
|
padding-left: 5px;
|
||||||
|
}
|
||||||
|
input[type="date"] {
|
||||||
|
-webkit-appearance: none;
|
||||||
|
-moz-appearance: none;
|
||||||
|
appearance: none;
|
||||||
|
display: block;
|
||||||
|
width: 100%;
|
||||||
|
padding: 10px;
|
||||||
|
line-height: 20px;
|
||||||
|
font-size: 16px;
|
||||||
|
font-weight: inherit;
|
||||||
|
background: #fff;
|
||||||
|
border-radius: 5px;
|
||||||
|
border: 1px solid #bdbdbd;
|
||||||
|
}
|
||||||
.container {
|
.container {
|
||||||
max-width: 1100px !important;
|
max-width: 1100px !important;
|
||||||
padding-left: 15px !important;
|
padding-left: 15px !important;
|
||||||
|
|||||||
@ -1,24 +1,37 @@
|
|||||||
function updatePost() {
|
function updatePost() {
|
||||||
const modal = document.querySelector('.modal-mask'),
|
const modal = document.querySelector('.modal-mask'),
|
||||||
btnShowModal = document.querySelectorAll('.update-post'),
|
btnShowModal = document.querySelectorAll('.update-post'),
|
||||||
btnClose = document.querySelector('.btn-close');
|
btnClose = document.querySelector('.btn-close'),
|
||||||
|
form = modal.querySelector('form');
|
||||||
|
|
||||||
btnShowModal.forEach(btn => {
|
btnShowModal.forEach(btn => {
|
||||||
btn.addEventListener('click', (e) => {
|
btn.addEventListener('click', (e) => {
|
||||||
modal.style.display = 'flex';
|
const btnSend = modal.querySelector('.btn-send');
|
||||||
|
id = e.target.getAttribute('data-id'),
|
||||||
|
idInput = modal.querySelector('#id');
|
||||||
|
|
||||||
const form = modal.querySelector('form');
|
idInput.setAttribute('name', 'id'); // post запрос id изменяемой записи
|
||||||
const id = e.target.getAttribute('data-id');
|
idInput.setAttribute('value', id); // value id изменяемой записи
|
||||||
|
modal.style.display = 'flex'; // показать модальное окно
|
||||||
|
|
||||||
form.elements[0].setAttribute('name', 'id'); // post запрос id изменяемой записи
|
btnSend.addEventListener('click', (e) => {
|
||||||
form.elements[0].setAttribute('value', id); // value id изменяемой записи
|
let idInputValue = idInput.value;
|
||||||
|
|
||||||
console.log(form.elements[0]);
|
if (!isNaN(idInputValue) == false) {
|
||||||
form.setAttribute('action', '/update');
|
alert('Поле ID должен быть числом!');
|
||||||
|
e.preventDefault();
|
||||||
|
} else {
|
||||||
|
idInputValue = idInputValue.trim();
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
btnClose.addEventListener('click', () => {
|
btnClose.addEventListener('click', (e) => {
|
||||||
|
e.preventDefault();
|
||||||
|
for (let i = 1; i < 4; i++) {
|
||||||
|
form.elements[i].value = '';
|
||||||
|
}
|
||||||
modal.style.display = 'none';
|
modal.style.display = 'none';
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,11 +3,15 @@ div(class="modal-mask")
|
|||||||
div(class="modal-head")
|
div(class="modal-head")
|
||||||
p(class="modal-title") Изменить запись
|
p(class="modal-title") Изменить запись
|
||||||
div(class="modal-body")
|
div(class="modal-body")
|
||||||
form(action="#" method="POST")
|
form(action="/update" method="POST")
|
||||||
input(type="text" value="id" placeholder="ID изменяемой записи")
|
label(for="id") ID изменяемой записи
|
||||||
input(type="text" name="title" placeholder="Название")
|
input(id="id" type="text" value="id" required)
|
||||||
input(type="text" name="date" placeholder="Дата")
|
label(for="title") Название
|
||||||
textarea(type="text" name="description" placeholder="Описание")
|
input(id="title" type="text" name="title" placeholder="Запись" required)
|
||||||
|
label(for="date") Дата
|
||||||
|
input(id="date" type="date" name="date" required)
|
||||||
|
label(for="description") Описание
|
||||||
|
textarea(id="description" type="text" name="description" placeholder="Описание" required)
|
||||||
div(class="modal-footer")
|
div(class="modal-footer")
|
||||||
button(class="button-primary") Принять
|
button(class="button-primary btn-send" type="submit") Принять
|
||||||
button(class="button-danger btn-close") Закрыть окно
|
button(class="button-danger btn-close") Закрыть окно
|
||||||
Loading…
x
Reference in New Issue
Block a user