How can the label of an HTML button be changed using PHP upon clicking?

To change the label of an HTML button using PHP upon clicking, you can utilize JavaScript to send a request to the server and update the button label based on the response from the server. This can be achieved by creating an AJAX request that calls a PHP script to update the button label and then returns the new label to be displayed.

<?php
if(isset($_POST['newLabel'])) {
    // Update the button label
    $newLabel = $_POST['newLabel'];
    echo $newLabel;
    exit;
}
?>

<!DOCTYPE html>
<html>
<head>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
    <button id="myButton">Click me</button>

    <script>
        $(document).ready(function() {
            $('#myButton').click(function() {
                $.ajax({
                    type: 'POST',
                    url: 'updateLabel.php',
                    data: { newLabel: 'New Label' },
                    success: function(response) {
                        $('#myButton').text(response);
                    }
                });
            });
        });
    </script>
</body>
</html>