Skip to content

Instantly share code, notes, and snippets.

@malchata
Last active December 23, 2020 19:37
Show Gist options
  • Save malchata/4aeb36585cd312f33ae0cff736a79fcc to your computer and use it in GitHub Desktop.
Save malchata/4aeb36585cd312f33ae0cff736a79fcc to your computer and use it in GitHub Desktop.
Stateless component in PHP
<?php
function SubscribeButton ($subscribed, $currentUserId, $userId, $className = null) {
if (!isset($userId) || !ctype_digit($userId) || $userId === $currentUserId) {
return "";
}
$subscribeButtonClasses = "subscribe-button";
if (!is_null($className)) {
$subscribeButtonClasses .= $className;
}
?>
<form method="POST" data-subscribed="<?php echo strval($subscribed); ?>" action="/subscribe">
<button type="submit" class="<?php echo $subscribeButtonClasses; ?>">
<?php echo $subscribed ? "Subscribed" : "Subscribe"; ?>
</button>
<input type="hidden" name="user-id" value="<?php echo strval($userId); ?>">
</form>
<?php
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment