Created
March 21, 2018 07:58
-
-
Save Nikolasgrizli/1320d359895f7d5084e2b91d93a51087 to your computer and use it in GitHub Desktop.
button like ajax + localStorage + php
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
<button class="supvideo">Мне нравится!</button> | |
<p> Количество просмотров <output id="statlike" >--</output></p> |
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 | |
if (isset($_POST['name'])) { | |
$fp = fopen('count.json', 'w'); | |
fwrite($fp, json_encode($_POST['name'])); | |
fclose($fp); | |
} else { | |
return false; | |
} | |
?> |
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
jQuery(function ($) { | |
$('document').ready( function(){ | |
$.getJSON('/count.json', function (data) { | |
console.log(data); | |
$('#statlike').text(data); | |
}); | |
}); | |
$('.supvideo').on('click', function(){ | |
if(localStorage.getItem('here') == 'was here' ){ | |
console.log('was here'); | |
} else { | |
var count= parseInt($('#statlike').text()); | |
count+=1; | |
localStorage.setItem('here', 'was here'); | |
console.log(localStorage.getItem('here')); | |
$.ajax({ | |
url : '/like_add.php', | |
method : 'post', | |
data : { 'name': count }, | |
success : function( response ) { | |
} | |
}); | |
$.getJSON('/count.json', function (data) { | |
console.log(data); | |
$('#statlike').text(data); | |
}); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment