How can PHP be used to distinguish between different submit buttons within a form to perform different actions?

To distinguish between different submit buttons within a form in PHP, you can use the name attribute of the submit buttons. When a form is submitted, you can check which submit button was clicked by checking the value of the name attribute. Based on this value, you can perform different actions or logic in your PHP script.

<form method="post">
    <button type="submit" name="action" value="save">Save</button>
    <button type="submit" name="action" value="delete">Delete</button>
</form>

<?php
if(isset($_POST['action'])){
    $action = $_POST['action'];
    
    if($action == 'save'){
        // Perform save action
    } elseif($action == 'delete'){
        // Perform delete action
    }
}
?>