Skip to content

Instantly share code, notes, and snippets.

@alxfv
Created February 20, 2014 08:11
Show Gist options
  • Save alxfv/9108927 to your computer and use it in GitHub Desktop.
Save alxfv/9108927 to your computer and use it in GitHub Desktop.
<?php
session_start();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (!empty($_SESSION['favourite'])) {
unset($_SESSION['favourite']);
$text = 'Добавить в избранное';
}
else {
$_SESSION['favourite'] = TRUE;
$text = 'Убрать из избранного';
}
header('Content-Type: application/json');
echo json_encode(array('text' => $text));
exit();
}
if (!empty($_SESSION['favourite'])) {
echo '<button>Убрать из избранного</button>';
}
else {
echo '<button>Добавить в избранное</button>';
}
?>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
$('button').click(function(e) {
e.preventDefault();
$.ajax({
url: '/',
method: 'post',
success: function(data) {
$('button').text(data.text);
}
});
});
})
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment