-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd.html
More file actions
131 lines (122 loc) · 5.77 KB
/
add.html
File metadata and controls
131 lines (122 loc) · 5.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&display=swap" rel="stylesheet">
<link rel="stylesheet" href="styles.css">
<title>Ajouter/Modifier un Produit</title>
</head>
<body>
<nav class="navbar">
<ul>
<li><a href="index.html">Accueil</a></li>
<li><a href="list.html">Liste</a></li>
<li><a href="search.html">Recherche</a></li>
</ul>
</nav>
<div class="container">
<div class="card">
<h1>Ajouter ou modifier un produit</h1>
<form id="productForm" class="product-form">
<input type="hidden" id="productId">
<div class="form-group">
<label for="productName">Nom du produit</label>
<input type="text" id="productName" placeholder="Nom du produit" required>
</div>
<div class="form-group">
<label for="productCategory">Catégorie</label>
<input type="text" id="productCategory" placeholder="Catégorie" required>
</div>
<div class="form-group">
<label for="productDescription">Description</label>
<textarea id="productDescription" placeholder="Description" required></textarea>
</div>
<div class="form-group">
<label for="productPrice">Prix</label>
<input type="number" id="productPrice" placeholder="Prix" step="0.01" required>
</div>
<div class="form-group">
<label for="productStock">Stock</label>
<input type="number" id="productStock" placeholder="Quantité en stock" required>
</div>
<div class="button-group">
<button type="submit" id="submitButton" class="primary">Ajouter</button>
<button type="button" id="cancelButton" class="secondary" style="display: none;">Annuler</button>
</div>
</form>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const form = document.getElementById('productForm');
const submitButton = document.getElementById('submitButton');
const cancelButton = document.getElementById('cancelButton');
// Vérifier si nous sommes en mode édition
const urlParams = new URLSearchParams(window.location.search);
const productId = urlParams.get('id');
if (productId) {
// Mode édition
submitButton.textContent = 'Modifier';
cancelButton.style.display = 'block';
loadProductData(productId);
}
form.addEventListener('submit', function(event) {
event.preventDefault();
addOrUpdateProduct();
});
cancelButton.addEventListener('click', function() {
window.location.href = 'list.html';
});
function loadProductData(id) {
fetch(`api.php?id=${id}`)
.then(response => response.json())
.then(product => {
document.getElementById('productId').value = product.id;
document.getElementById('productName').value = product.nom;
document.getElementById('productCategory').value = product.categorie;
document.getElementById('productDescription').value = product.description;
document.getElementById('productPrice').value = product.prix;
document.getElementById('productStock').value = product.stock;
})
.catch(error => {
console.error('Erreur:', error);
alert('Erreur lors du chargement du produit');
});
}
function addOrUpdateProduct() {
const productId = document.getElementById('productId').value;
const newProduct = {
id: productId ? parseInt(productId) : null,
nom: document.getElementById('productName').value,
categorie: document.getElementById('productCategory').value,
description: document.getElementById('productDescription').value,
prix: parseFloat(document.getElementById('productPrice').value),
stock: parseInt(document.getElementById('productStock').value),
action: productId ? 'update' : 'add'
};
fetch('api.php', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(newProduct),
})
.then(response => response.json())
.then(data => {
if (data.success) {
alert(productId ? 'Produit modifié avec succès!' : 'Produit ajouté avec succès!');
window.location.href = 'list.html';
} else {
alert('Erreur lors de l\'opération');
}
})
.catch(error => {
console.error('Erreur:', error);
alert('Erreur lors de l\'opération');
});
}
});
</script>
</body>
</html>