How can an alert button be used to warn users about losing input data when leaving a page in PHP?

When a user has entered input data on a form but tries to leave the page without submitting it, there is a risk of losing that data. To prevent this, an alert button can be used to warn users before they navigate away from the page. This alert can prompt users to confirm if they want to leave the page and potentially lose their input data.

<script>
    window.onbeforeunload = function() {
        return "Are you sure you want to leave this page? Your input data may not be saved.";
    };
</script>