Created
August 15, 2018 23:37
-
-
Save skylerkatz/98d43025c89db327fb3966697db0a14f to your computer and use it in GitHub Desktop.
Use AJAX to submit prayer acknowledgement link
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 | |
$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