How can PHP code dynamically change the action attribute of a form based on which submit button is clicked?

To dynamically change the action attribute of a form based on which submit button is clicked, you can use JavaScript to set the form's action attribute before submitting the form. You can achieve this by adding an onclick event to each submit button that updates the action attribute accordingly.

<form id="myForm" method="post" action="">
    <input type="submit" name="submit1" value="Submit 1" onclick="document.getElementById('myForm').action = 'action1.php';">
    <input type="submit" name="submit2" value="Submit 2" onclick="document.getElementById('myForm').action = 'action2.php';">
</form>