- added redirect after registration

- WIP: order system to save orders and display them to the buyer
This commit is contained in:
Fabian 2025-04-27 13:37:15 +02:00
parent c3636b0166
commit 34907bbdb5
4 changed files with 24 additions and 18 deletions

View File

@ -39,36 +39,34 @@
<label for="ArtikelNr">Artikel Nr.:</label>
<textarea id="ArtikelNr" name="ArtikelNr" rows="4" required placeholder="Artikel Nr."></textarea>
<a href="/bestellung" class="button-submit" id="sendOrder">Bestellung absenden</a>
<a class="button-submit" id="sendOrder">Bestellung absenden</a>
</form>
</div>
</main>
<script>
document.getElementById('bestellform').addEventListener('sendOrder', async (event) => {
document.getElementById('bestellform').addEventListener('click', async (event) => {
event.preventDefault(); // Verhindert das Standardformularverhalten
const userId = document.getElementById('KundenNr').value;
const productId = document.getElementById('ArtikelNr').value;
const formData = {
user_id: document.getElementById('KundenNr').value,
product_id: document.getElementById('ArtikelNr').value,
}
console.log(formData)
try {
const response = await fetch('/api/bestellung', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
userId,
productId,
})
body: JSON.stringify(formData)
});
const result = await response.json();
if (response.ok) {
alert('Bestellung erfolgreich!');
window.location.href = '/bestellung.html'; // Weiterleitung zur Bestellübersicht
//window.location.href = '/bestellung.html'; // Weiterleitung zur Bestellübersicht
} else {
alert('Fehler bei der Bestellung: ' + result.message);
}

View File

@ -32,6 +32,7 @@
if (!user_id){
console.log("is null")
alert('Sie sind nicht eingeloggt! Bitte loggen sie sich ein damit diese Seite angezeigt werden kann.')
window.location.href = '/login'
} else if (user_id <= 1) {
try {
const response = await fetch('/api/bestellung/daten', {
@ -44,6 +45,7 @@
if (response.ok) {
const data = await response.json();
console.log(data)
document.getElementById('kundenNr').textContent = user_id
document.getElementById('produktId').textContent = data.product_id
//document.getElementById('produktName').textContent = data.

View File

@ -77,6 +77,7 @@
const data = await response.json();
sessionStorage.setItem("user_id", data.id)
alert('Nutzer erfolgreich hinzugefügt! Ihre Kundennummer: ' + data.id)
window.location.href= '/'
} else {
alert('Fehler bei der Registrierung.')
}

View File

@ -171,10 +171,15 @@ app.post('/api/user/login', (req, res) => {
app.post('/api/bestellung', (req, res) => {
const {user_id, product_id} = req.body;
const sql = 'INSERT INTO order_items (user_id, product_id, quantity) VALUES (?, ?, 1)'
const sql1 = 'INSERT INTO order_details (, user_id, payment_id, total) VALUES (?, null, null)'
const sql2 = 'SELECT id FROM order_details WHERE user_id = ?'
const sql3 = 'INSERT INTO order_items (user_id, product_id, quantity, order_id) VALUES (?, ?, 1, ?)'
db.query(sql, [user_id, product_id], (err, results) => {
db.query(sql1, [user_id])
const id = db.query(sql2, [user_id])
db.query(sql3, [user_id, product_id], (err, results) => {
if (err || results.length === 0) {
console.error('Fehler beim Abrufen des Produkts: ', err);
return res.status(500).json({message: 'Produkt nicht gefunden oder Serverfehler'});
@ -187,7 +192,7 @@ app.get('/api/bestellung/daten', (req, res) => {
const user_id = req.body;
const sql = 'SELECT * FROM order_items WHERE user_id = ? '
const sql = 'SELECT oi.user_id, oi.product_id, p.product_name, p.price FROM order_items oi INNER JOIN product p ON oi.product_id = p.id WHERE user_id = ? '
db.query(sql, [user_id], (err, results) => {
if (err) {
console.error('Fehler beim Abrufen der Bestellung: ', err);