<form id="putForm">
    <label for="feld1">Feld 1:</label>
    <input type="text" id="feld1" name="feld1" value="roscher@wvss-mannheim.de" required>
    <br>
    <label for="feld2">Feld 2:</label>
    <input type="text" id="feld2" name="feld2" value="roscher@wvs-mannheim.de" required>
    <br>
    <button type="submit">Senden</button>
</form>

<div id="result"></div> <!-- Platz für die Rückmeldung -->

<script>
   document.getElementById("putForm").addEventListener("submit", async function (event) {
    event.preventDefault(); // Verhindert das Standardformularverhalten

    const feld1 = document.getElementById("feld1").value;
    const feld2 = document.getElementById("feld2").value;

    const formData = new URLSearchParams();
    formData.append("feld1", feld1);
    formData.append("feld2", feld2);

    try {
        const response = await fetch("http://localhost:5002/update-daten", {
            method: "PUT",
            headers: {
                "Content-Type": "application/x-www-form-urlencoded",
            },
            body: formData.toString(),
        });

        // Prüfe den Content-Type der Antwort
        const contentType = response.headers.get("Content-Type");

        let result;
        if (contentType && contentType.includes("application/json")) {
            result = await response.json(); // JSON-Antwort parsen
        } else {
            result = await response.text(); // Textantwort lesen
        }

        const resultDiv = document.getElementById("result");

        if (response.ok) {
            resultDiv.textContent = "Erfolg: " + (result.message || result);
            resultDiv.style.color = "green";
        } else {
            resultDiv.textContent = "Fehler: " + (result.error || result);
            resultDiv.style.color = "red";
        }
    } catch (error) {
        console.error("Anfragefehler:", error);
        const resultDiv = document.getElementById("result");
        resultDiv.textContent = "Fehler beim Senden der Anfrage: " + error.message;
        resultDiv.style.color = "red";
    }
});

</script>
