document.addEventListener('DOMContentLoaded', function () { const beautifulForm = document.getElementById('beautiful-form'); const funnyForm = document.getElementById('funny-form'); const toastLiveExample = document.getElementById('liveToast'); const toast = new bootstrap.Toast(toastLiveExample); const handleFormSubmit = (form, event) => { event.preventDefault(); const formData = new FormData(form); const data = Object.fromEntries(formData.entries()); fetch('submit_vote.php', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(data), }) .then(response => response.json()) .then(data => { if (data.success) { toast.show(); form.reset(); } else { alert('Error: ' + data.error); } }) .catch((error) => { console.error('Error:', error); alert('An error occurred while submitting your votes.'); }); }; beautifulForm.addEventListener('submit', (event) => handleFormSubmit(beautifulForm, event)); funnyForm.addEventListener('submit', (event) => handleFormSubmit(funnyForm, event)); });