Skip to content
PHP Logo

How can a checkbox in PHP trigger a POST request to a specific PHP file?

To trigger a POST request to a specific PHP file when a checkbox is clicked, you can use JavaScript to listen for the checkbox change event and then submit a form with the desired PHP file as the action. This form submission will trigger a POST request to the specified PHP file.

php
<form id="checkboxForm" action="specific_php_file.php" method="post">
    <input type="checkbox" id="myCheckbox" name="myCheckbox"> Check me
</form>

<script>
document.getElementById('myCheckbox').addEventListener('change', function() {
    document.getElementById('checkboxForm').submit();
});
</script>