Created
February 20, 2014 08:11
-
-
Save alxfv/9108927 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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