Separated javascript from index.php
This commit is contained in:
parent
9ad6876181
commit
51097f79c3
2 changed files with 198 additions and 192 deletions
195
script.php
Normal file
195
script.php
Normal file
|
|
@ -0,0 +1,195 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<script>
|
||||
document.getElementById('bookForm').addEventListener('submit', function (e) {
|
||||
e.preventDefault(); // prevent normal form submission
|
||||
|
||||
const formData = new FormData(this);
|
||||
|
||||
fetch('submission.php', {
|
||||
method: 'POST',
|
||||
body: formData
|
||||
})
|
||||
.then(response => response.text())
|
||||
.then(data => {
|
||||
document.getElementById('result').innerHTML = data; // show success/error
|
||||
this.reset(); // reset form after success
|
||||
})
|
||||
.catch(error => {
|
||||
document.getElementById('result').innerHTML = 'Error: ' + error;
|
||||
});
|
||||
});
|
||||
|
||||
document.getElementById('copyForm').addEventListener('submit', function (e) {
|
||||
e.preventDefault(); // prevent normal form submission
|
||||
|
||||
const formData = new FormData(this);
|
||||
|
||||
fetch('submission.php', {
|
||||
method: 'POST',
|
||||
body: formData
|
||||
})
|
||||
.then(response => response.text())
|
||||
.then(data => {
|
||||
document.getElementById('result').innerHTML = data; // show success/error
|
||||
this.reset(); // optional: reset form after success
|
||||
})
|
||||
.catch(error => {
|
||||
document.getElementById('result').innerHTML = 'Error: ' + error;
|
||||
});
|
||||
});
|
||||
|
||||
document.getElementById('borrowerForm').addEventListener('submit', function (e) {
|
||||
e.preventDefault(); // prevent normal form submission
|
||||
|
||||
const formData = new FormData(this);
|
||||
|
||||
fetch('submission.php', {
|
||||
method: 'POST',
|
||||
body: formData
|
||||
})
|
||||
.then(response => response.text())
|
||||
.then(data => {
|
||||
document.getElementById('result').innerHTML = data; // show success/error
|
||||
this.reset(); // optional: reset form after success
|
||||
})
|
||||
.catch(error => {
|
||||
document.getElementById('result').innerHTML = 'Error: ' + error;
|
||||
});
|
||||
});
|
||||
|
||||
document.getElementById('loanForm').addEventListener('submit', function (e) {
|
||||
e.preventDefault(); // prevent normal form submission
|
||||
|
||||
const formData = new FormData(this);
|
||||
|
||||
fetch('submission.php', {
|
||||
method: 'POST',
|
||||
body: formData
|
||||
})
|
||||
.then(response => response.text())
|
||||
.then(data => {
|
||||
document.getElementById('result').innerHTML = data; // show success/error
|
||||
this.reset(); // optional: reset form after success
|
||||
})
|
||||
.catch(error => {
|
||||
document.getElementById('result').innerHTML = 'Error: ' + error;
|
||||
});
|
||||
|
||||
location.reload();
|
||||
});
|
||||
|
||||
document.getElementById('checkInForm').addEventListener('submit', function (e) {
|
||||
e.preventDefault(); // prevent normal form submission
|
||||
|
||||
const formData = new FormData(this);
|
||||
|
||||
fetch('submission.php', {
|
||||
method: 'POST',
|
||||
body: formData
|
||||
})
|
||||
.then(response => response.text())
|
||||
.then(data => {
|
||||
document.getElementById('result').innerHTML = data; // show success/error
|
||||
this.reset(); // optional: reset form after success
|
||||
})
|
||||
.catch(error => {
|
||||
document.getElementById('result').innerHTML = 'Error: ' + error;
|
||||
});
|
||||
|
||||
location.reload();
|
||||
});
|
||||
|
||||
document.getElementById('sqlSelectInputForm').addEventListener('submit', function (e) {
|
||||
e.preventDefault(); // prevent normal form submission
|
||||
|
||||
const formData = new FormData(this);
|
||||
|
||||
fetch('submission.php', {
|
||||
method: 'POST',
|
||||
body: formData
|
||||
})
|
||||
.then(response => response.text())
|
||||
.then(data => {
|
||||
const container = document.getElementById('result');
|
||||
|
||||
container.innerHTML = data; // show success/error
|
||||
this.reset(); // optional: reset form after success
|
||||
|
||||
// scroll after the new content is in place
|
||||
if (container) {
|
||||
container.scrollIntoView({ behavior: 'smooth' });
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
document.getElementById('result').innerHTML = 'Error: ' + error;
|
||||
});
|
||||
});
|
||||
|
||||
document.querySelector("#bookSelect").addEventListener("change", function () {
|
||||
const csrf_token = "<?= $_SESSION['csrf_token'] ?>";
|
||||
const bookID = this.value;
|
||||
const copySelect = document.querySelector("#copySelect");
|
||||
|
||||
// reset exemplar list
|
||||
copySelect.innerHTML = '<option value="">Lade Exemplare...</option>';
|
||||
|
||||
if (bookID) {
|
||||
fetch("submission.php?submissionType=" + encodeURI("getCopies") + "&bookID=" + encodeURIComponent(bookID) + "&csrf_token=" + encodeURIComponent(csrf_token))
|
||||
.then(response => response.json())
|
||||
.then(copies => {
|
||||
copySelect.innerHTML = "";
|
||||
if (copies.length === 0) {
|
||||
copySelect.innerHTML = '<option value="">Keine Exemplare verfügbar</option>';
|
||||
} else {
|
||||
copies.forEach(copy => {
|
||||
const opt = document.createElement("option");
|
||||
opt.value = copy.copyID;
|
||||
opt.textContent = "Exemplar #" + copy.copyID + " (" + copy.bookCondition + ")";
|
||||
copySelect.appendChild(opt);
|
||||
});
|
||||
}
|
||||
})
|
||||
.catch(err => {
|
||||
copySelect.innerHTML = '<option value="">Fehler beim Laden</option>';
|
||||
console.error(err);
|
||||
});
|
||||
} else {
|
||||
copySelect.innerHTML = '<option value="">Bitte zuerst ein Buch wählen</option>';
|
||||
}
|
||||
});
|
||||
|
||||
document.querySelector("#borrowerSelect").addEventListener("change", function () {
|
||||
const csrf_token = "<?= $_SESSION['csrf_token'] ?>";
|
||||
const borrowerID = this.value;
|
||||
const copySelect = document.querySelector("#copyReturnSelect");
|
||||
|
||||
// reset copy list
|
||||
copySelect.innerHTML = '<option value="">Lade Exemplare...</option>';
|
||||
|
||||
if (borrowerID) {
|
||||
fetch("submission.php?submissionType=" + encodeURI("getReturnCopies") + "&borrowerID=" + encodeURIComponent(borrowerID) + "&csrf_token=" + encodeURIComponent(csrf_token))
|
||||
.then(response => response.json())
|
||||
.then(copies => {
|
||||
copySelect.innerHTML = "";
|
||||
if (copies.length === 0) {
|
||||
copySelect.innerHTML = '<option value="">Keine Exemplare ausgeliehen</option>';
|
||||
} else {
|
||||
copies.forEach(copy => {
|
||||
const opt = document.createElement("option");
|
||||
opt.value = copy.copyID + "-" + copy.loanID;
|
||||
opt.textContent = copy.bookTitle + " von " + copy.bookAuthor + " Exemplar #" + copy.copyID + " (" + copy.bookCondition + ")";
|
||||
copySelect.appendChild(opt);
|
||||
});
|
||||
}
|
||||
})
|
||||
.catch(err => {
|
||||
copySelect.innerHTML = '<option value="">Fehler beim Laden</option>';
|
||||
console.error(err);
|
||||
});
|
||||
} else {
|
||||
copySelect.innerHTML = '<option value="">Bitte zuerst einen Ausleiher wählen</option>';
|
||||
}
|
||||
});
|
||||
</script>
|
||||
</html>
|
||||
Loading…
Add table
Add a link
Reference in a new issue