Hej męcze się ze skryptem Like / Dislike z podmianką dynamiczną na nową wartość liczbową. Kiedyś rozwiązałem ten problem ale nie wiem dlaczego zamiast zwracać numer +1 to zwraca mi całą stronę jako kod.
Głosy są zaliczane ale coś jest skopane z odbieraniem ich.
Fragment odpowiedzialny za przyciski like and dislike
<ul class="voting_wrapper" id="'.$result['id'].'"> <a class="up_button"><i class="icon_like_alt"></i></a> <span class="up_votes">'.$result['glos_plus'].'
</span>
<a class="down_button"><i class="icon_dislike_alt"></i></a> <span class="down_votes">'.$result['glos_minus'].'
</span>
skrypt
$(document).ready(function() {
$.each( $('.voting_wrapper'), function(){
var unique_id = $(this).attr("id");
postdata = {'opinia':'glosuj','unique_id':unique_id, 'vote':'fetch'};
$.post('includes/ajax-reviews-votes.php', postdata, function(response) {
$('#'+unique_id+' .up_votes').text(response.vote_up);
$('#'+unique_id+' .down_votes').text(response.vote_down);
},'json');
});
$(".voting_wrapper .voting_btn").click(function (e) {
var clicked_button = $(this).children().attr('class');
var unique_id = $(this).parent().attr("id");
if(clicked_button==='down_button')
{
postdata = {'opinia':'glosuj','unique_id':unique_id, 'vote':'down'};
$.post('includes/ajax-reviews-votes.php', postdata, function(data) {
$('#'+unique_id+' .down_votes').text(data);
}).fail(function(err) {
});
}
else if(clicked_button==='up_button') //user liked the content
{
postdata = {'opinia':'glosuj', 'unique_id':unique_id, 'vote':'up'};
$.post('includes/ajax-reviews-votes.php', postdata, function(data) {
$('#'+unique_id+' .up_votes').text(data);
// alert("Thanks! For Liking This Content.");
//AlertSukces('
<i class ="fa fa-check-square-o" aria-hidden="true"></i> Dziękujemy za pozytywną opinię!');
}).fail(function(err) {
//alert(err.statusText);
//AlertUwaga('
<i class="fa fa-check-square-o" aria-hidden="true"></i> Głos został już wcześniej oddany!');
//AlertInfo('
<i class="fa fa-check-square-o" aria-hidden="true"></i> Głos nie został dodany!.');
});
}
});
});
Plik ajax-reviews-votes.php
switch ($user_vote_type)
{
case 'up':
$result = mysqli_query($sql_con,"SELECT glos_plus FROM reviews WHERE id='$unique_content_id' LIMIT 1");
$get_total_rows = mysqli_fetch_assoc($result);
if($get_total_rows)
{
mysqli_query($sql_con,"UPDATE reviews SET glos_plus = glos_plus+1 WHERE id='$unique_content_id'");
}
echo ($get_total_rows["glos_plus"]+1
); //display total liked votes
break;
##### User disliked the content #########
case 'down':
$result = mysqli_query($sql_con,"SELECT glos_minus FROM reviews WHERE id='$unique_content_id' LIMIT 1");
$get_total_rows = mysqli_fetch_assoc($result);
if($get_total_rows)
{
mysqli_query($sql_con,"UPDATE reviews SET glos_minus=glos_minus+1 WHERE id='$unique_content_id'");
}
if($get_total_rows) {
echo ($get_total_rows_old["glos_minus"]+1
); //display total liked votes }
break;
case 'fetch':
$result = mysqli_query($sql_con,"SELECT glos_plus,glos_minus FROM reviews WHERE id ='$unique_content_id' LIMIT 1");
$row = mysqli_fetch_assoc($result);
$vote_up = ($row["glos_plus"])?$row["glos_plus"]:0;
$vote_down = ($row["glos_minus"])?$row["glos_minus"]:0;
$send_response = array('glos_plus'=>$vote_up, 'glos_minus'=>$vote_down);
echo json_encode
($send_response); break;
}
Ten post edytował designez 20.11.2018, 20:13:18