How can PHP developers ensure that a file is only cleared upon a button click event?

To ensure that a file is only cleared upon a button click event, PHP developers can use a conditional check to determine if the button has been clicked before clearing the file. This can be achieved by checking if the button's corresponding form has been submitted. If the form has been submitted, then the file can be cleared. Otherwise, the file should not be cleared.

<?php
if(isset($_POST['clear_button'])) {
    // Code to clear the file
    file_put_contents('file.txt', '');
    echo "File cleared successfully.";
}
?>

<form method="post">
    <input type="submit" name="clear_button" value="Clear File">
</form>