Newer
Older
rails-otp-try / CPM-program / app / javascript / quiz-response.js
@ItoRino ItoRino on 1 Dec 1 KB add


let buttonCount = 0;

function onButtonClick() {
  buttonCount++;
  updateButtonCountOnServer(buttonCount);
}

function updateButtonCountOnServer(count) {
  fetch('/quiz_responses/show', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'X-CSRF-Token': document.querySelector('meta[name="csrf-token"]').content,
    },
    body: JSON.stringify({ count: count }),
  })
    .then(response => response.json())
    .then(data => {
      if (data.status === 'success') {
        console.log('Button count updated successfully!');
      } else {
        console.error('Error updating button count:', data.errors);
      }
    })
    .catch(error => console.error('Error updating button count:', error));
}

function updateButtonCountDisplay() {
    fetch('/quiz_responses/show')
      .then(response => response.text())
      .then(data => {
        // クイズページ内の適切なエレメントに回数を反映
        document.getElementById('buttonCountDisplay').innerHTML = data;
      })
      .catch(error => console.error('Error updating button count display:', error));
  }