diff --git a/test.html b/test.html index f8772e9..6fed272 100644 --- a/test.html +++ b/test.html @@ -36,21 +36,23 @@ }); }); - // 2. Charger les produits via l'action correcte + // 2. Charger les produits fetch('api/index.php?action=products_list') .then(r => r.json()) .then(data => { + console.log("DEBUG PRODUITS :", data); // Ouvre la console F12 pour voir ça ! const sel = document.getElementById('product-selector'); - // On utilise 'data' directement si c'est un tableau, ou la clé correcte const prods = data.products || data; - prods.forEach(p => { - const opt = document.createElement('option'); - opt.value = p.id; - opt.setAttribute('data-name', p.name); - opt.setAttribute('data-cat', p.category || ''); - opt.textContent = p.name; - sel.appendChild(opt); - }); + if(Array.isArray(prods)) { + prods.forEach(p => { + const opt = document.createElement('option'); + opt.value = p.id; + opt.setAttribute('data-name', p.name); + opt.setAttribute('data-cat', p.category || ''); + opt.textContent = p.name; + sel.appendChild(opt); + }); + } }); function updateIdField() { @@ -61,6 +63,7 @@ document.getElementById('category-selector').value = opt.getAttribute('data-cat'); } + // 3. Fonction unique de sauvegarde function saveProduct() { const data = { id: document.getElementById('prod-id').value, @@ -68,8 +71,6 @@ category: document.getElementById('category-selector').value }; - console.log("Données envoyées :", data); // Vérifie ceci dans ta console F12 - fetch('api/index.php?action=product_save', { method: 'POST', headers: {'Content-Type': 'application/json'}, @@ -77,22 +78,10 @@ }) .then(r => r.json()) .then(res => { - // Affiche tout l'objet de réponse pour voir le message d'erreur précis - console.log("RÉPONSE COMPLÈTE DE L'API :", res); - alert("Réponse reçue (voir console F12) : " + JSON.stringify(res)); + console.log("RÉPONSE API :", res); + alert("Réponse reçue. Regarde la console (F12) pour le détail."); }) - .catch(e => console.error("Erreur réseau :", e)); - } - - // On utilise l'action 'product_save' identifiée par ton grep - fetch('api/index.php?action=product_save', { - method: 'POST', - headers: {'Content-Type': 'application/json'}, - body: JSON.stringify(data) - }) - .then(r => r.json()) - .then(res => alert("Réponse API : " + JSON.stringify(res))) - .catch(e => alert("Erreur : " + e)); + .catch(e => console.error("Erreur :", e)); }