How can JavaScript be utilized to manipulate checkbox elements in a PHP form?

To manipulate checkbox elements in a PHP form using JavaScript, you can use JavaScript to listen for changes in the checkbox elements and update the form accordingly. This can be useful for dynamically updating the form based on user interactions with the checkboxes.

<form id="myForm">
  <input type="checkbox" name="option1" value="1"> Option 1
  <input type="checkbox" name="option2" value="2"> Option 2
</form>

<script>
  const checkboxes = document.querySelectorAll('input[type="checkbox"]');
  
  checkboxes.forEach(checkbox => {
    checkbox.addEventListener('change', function() {
      // Update form action based on checkbox state
      document.getElementById('myForm').action = 'process.php?option1=' + checkboxes[0].checked + '&option2=' + checkboxes[1].checked;
    });
  });
</script>