How can PHP be used to dynamically change the target URL based on form button clicks?

To dynamically change the target URL based on form button clicks in PHP, you can use JavaScript to handle the button click events and update the form's action attribute accordingly. This can be achieved by adding an onclick event handler to each button that updates the form's action attribute with the desired URL.

<form id="myForm" method="post">
    <button onclick="document.getElementById('myForm').action = 'page1.php';">Page 1</button>
    <button onclick="document.getElementById('myForm').action = 'page2.php';">Page 2</button>
    <input type="submit" value="Submit">
</form>