mirror of
https://github.com/sergeyyarkov/blog-expressjs.git
synced 2026-10-11 10:19:07 +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,
|
||||
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>`);
|
||||
});
|
||||
// UPDATE `blog`.`posts` SET `description` = 'gfhj' WHERE (`id` = '10');
|
||||
|
||||
@ -12,6 +12,23 @@ textarea {
|
||||
resize: none;
|
||||
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 {
|
||||
max-width: 1100px !important;
|
||||
padding-left: 15px !important;
|
||||
|
||||
@ -1,24 +1,37 @@
|
||||
function updatePost() {
|
||||
const modal = document.querySelector('.modal-mask'),
|
||||
btnShowModal = document.querySelectorAll('.update-post'),
|
||||
btnClose = document.querySelector('.btn-close');
|
||||
btnClose = document.querySelector('.btn-close'),
|
||||
form = modal.querySelector('form');
|
||||
|
||||
btnShowModal.forEach(btn => {
|
||||
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');
|
||||
const id = e.target.getAttribute('data-id');
|
||||
idInput.setAttribute('name', 'id'); // post запрос id изменяемой записи
|
||||
idInput.setAttribute('value', id); // value id изменяемой записи
|
||||
modal.style.display = 'flex'; // показать модальное окно
|
||||
|
||||
form.elements[0].setAttribute('name', 'id'); // post запрос id изменяемой записи
|
||||
form.elements[0].setAttribute('value', id); // value id изменяемой записи
|
||||
btnSend.addEventListener('click', (e) => {
|
||||
let idInputValue = idInput.value;
|
||||
|
||||
console.log(form.elements[0]);
|
||||
form.setAttribute('action', '/update');
|
||||
if (!isNaN(idInputValue) == false) {
|
||||
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';
|
||||
});
|
||||
}
|
||||
|
||||
@ -3,11 +3,15 @@ div(class="modal-mask")
|
||||
div(class="modal-head")
|
||||
p(class="modal-title") Изменить запись
|
||||
div(class="modal-body")
|
||||
form(action="#" method="POST")
|
||||
input(type="text" value="id" placeholder="ID изменяемой записи")
|
||||
input(type="text" name="title" placeholder="Название")
|
||||
input(type="text" name="date" placeholder="Дата")
|
||||
textarea(type="text" name="description" placeholder="Описание")
|
||||
form(action="/update" method="POST")
|
||||
label(for="id") ID изменяемой записи
|
||||
input(id="id" type="text" value="id" required)
|
||||
label(for="title") Название
|
||||
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")
|
||||
button(class="button-primary") Принять
|
||||
button(class="button-primary btn-send" type="submit") Принять
|
||||
button(class="button-danger btn-close") Закрыть окно
|
||||
Loading…
x
Reference in New Issue
Block a user