Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Use AJAX to submit prayer acknowledgement link
<?php
$prayers = getContent(
'prayer',
'display:list',
'before_show:__pagination__',
'emailencode:no',
'json',
'nocache'
);
?>
<?php foreach ($prayers as $prayer) { ?>
<a href="<?= $prayer['acknowledgmenturl'] ?>" data-prayer="<?= $prayer['id'] ?>" class="acknowledgement_link">I prayed for this</a>
<?php } ?>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
$(document).ready(function () {
$(".acknowledgement_link").each(function (index, link) {
var link = $(link);
var cookie = "acknowlegement_" + link.data('prayer');
if (cookieExists(cookie)) {
link.addClass('disabled');
}
});
$( ".acknowledgement_link" ).click(function( event ) {
event.preventDefault();
var link = $(this);
link.addClass('disabled');
cookie = "acknowlegement_" + link.data('prayer')
if (cookieExists(cookie)) {
return;
}
axios.get(link.attr('href'))
.then(function (response) {
$('#acknowledgment_count_' + link.data('prayer')).text(response.data.acknowledgement_count);
document.cookie = "acknowlegement_" + link.data('prayer') + "=" + true;
})
.catch(function (error) {
alert(error.response);
});
});
});
function cookieExists(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for(var i = 0; i <ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return true;
}
}
return false;
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment