How can PHP header function be utilized to redirect users based on the result of a database action triggered by a button click?

When a button is clicked, a database action is triggered, and based on the result of this action, we can use the PHP header function to redirect users to different pages. By checking the result of the database action, we can conditionally set the location header to redirect users accordingly.

// Perform database action here
// Check the result of the action
if ($result) {
    header("Location: success.php");
    exit();
} else {
    header("Location: error.php");
    exit();
}