Podszedł bym inaczej do tematu. Zmiana koloru tabeli powinna być niezależna od wysyłania danych, więc ustawił bym kolor tabeli na wybrany select i dopiero wysłał dane do bazy i ustawił status wysyłania na zakończone powodzeniem lub nie.
<table>
<thead>
<tr>
<th colspan="2">Kolorowanie kolumn</th>
</tr>
</thead>
<tbody>
<tr class="choiceBgColor">
<td>kolumna 1</td>
<td>kolumna 2</td>
</tr>
<tr>
<td>
<form>
<select id="column3">
<option>-Wybierz status</option>
<option value="red">Red</option>
<option value="green">Green</option>
<option value="yellow">Yellow</option>
</select>
</form>
</td>
</tr>
</tbody>
</table>
<div>Wybrałeś kolor: <span class="result"></span></div>
<div class="sendInfo"></div>
const sendData = (info) => {
const data = { id: "id", text: "text", column_name: "column_name" };
fetch("edit.php", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data),
})
.then((response) => response.json())
.then((data) => {
console.log("Success:", data);
info.textContent = "Dane zostały poprawnie wysłane";
})
.catch((error) => {
console.error("Error:", error);
info.textContent = "Dane nie zostały poprawnie wysłane";
});
};
const selectElement = document.querySelector("select");
const tr = document.querySelector(".choiceBgColor");
selectElement.addEventListener("change", (event) => {
const result = document.querySelector(".result");
const sendInfo = document.querySelector(".sendInfo");
tr.style.backgroundColor = event.target.value;
result.textContent = event.target.value;
result.style.color = event.target.value;
sendData(sendInfo);
});
Brakuje zabezpieczenia przed wybraniem pierwszej opcji '-Wybierz status' w select