<script>
function editRecord(button) {
var row = button.closest("tr");
var editInputs = row.querySelectorAll(".edit-input");
var editableElements = row.querySelectorAll(".editable");
editInputs.forEach(function (element) {
element.style.display = "inline-block";
});
editableElements.forEach(function (element) {
element.style.display = "none";
});
row.querySelector(".edit-btn").style.display = "none";
row.querySelector(".save-btn").style.display = "inline-block";
}
function saveRecord(button) {
var row = button.closest("tr");
var id = row.getAttribute("data-record-id");
var name = row.querySelector("[data-field='name']").value;
var email = row.querySelector("[data-field='email']").value;
var formData = new FormData();
formData.append("saveRecord", true);
formData.append("recordId", id);
formData.append("name", name);
formData.append("email", email);
fetch(window.location.href, {
method: "POST",
body: formData
})
.then(function(response) {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.text();
})
.then(function(data) {
console.log(data);
location.reload();
})
.catch(function(error) {
console.error('There has been a problem with your fetch operation:', error);
alert("Error saving record");
});
}
function deleteRecord(button) {
var row = button.closest("tr");
var id = row.getAttribute("data-record-id");
var formData = new FormData();
formData.append("deleteRecord", true);
formData.append("recordId", id);
fetch(window.location.href, {
method: "POST",
body: formData
})
.then(function(response) {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.text();
})
.then(function(data) {
console.log(data);
row.remove();
})
.catch(function(error) {
console.error('There has been a problem with your fetch operation:', error);
alert("Error deleting record");
});
}
function addRecord() {
var newName = document.getElementById('newName').value;
var newEmail = document.getElementById('newEmail').value;
var formData = new FormData();
formData.append("addRecord", true);
formData.append("name", newName);
formData.append("email", newEmail);
fetch(window.location.href, {
method: "POST",
body: formData
})
.then(function(response) {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.text();
})
.then(function(data) {
console.log(data);
location.reload();
})
.catch(function(error) {
console.error('There has been a problem with your fetch operation:', error);
alert("Error adding record");
});
}
// Добавляем обработчик события клика после полной загрузки документа
document.addEventListener('DOMContentLoaded', function () {
var addButton = document.getElementById('addButton');
if (addButton) {
addButton.addEventListener('click', addRecord);
}
});
</script>
Uncaught ReferenceError: $ is not defined
at addRecord (z.php:103:9)
at HTMLButtonElement.onclick (z.php:34:51) при добавлений записи