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
<http://december.com/html/4/element/ul.html class="voting_wrapper" id="'.$result['id'].'"> <http://december.com/html/4/element/li.html class="voting_btn"> <http://december.com/html/4/element/a.html class="up_button"><http://december.com/html/4/element/i.html class="icon_like_alt"></http://december.com/html/4/element/i.html></http://december.com/html/4/element/a.html> <http://december.com/html/4/element/span.html class="up_votes">'.$result['glos_plus'].'</http://december.com/html/4/element/span.html> </http://december.com/html/4/element/li.html> <http://december.com/html/4/element/li.html class="voting_btn"> <http://december.com/html/4/element/a.html class="down_button"><http://december.com/html/4/element/i.html class="icon_dislike_alt"></http://december.com/html/4/element/i.html></http://december.com/html/4/element/a.html> <http://december.com/html/4/element/span.html class="down_votes">'.$result['glos_minus'].'</http://december.com/html/4/element/span.html> </http://december.com/html/4/element/li.html> </http://december.com/html/4/element/ul.html>
$(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('<http://december.com/html/4/element/i.html class ="fa fa-check-square-o" aria-hidden="true"></http://december.com/html/4/element/i.html> Dziękujemy za pozytywną opinię!'); }).fail(function(err) { //alert(err.statusText); //AlertUwaga('<http://december.com/html/4/element/i.html class="fa fa-check-square-o" aria-hidden="true"></http://december.com/html/4/element/i.html> Głos został już wcześniej oddany!'); //AlertInfo('<http://december.com/html/4/element/i.html class="fa fa-check-square-o" aria-hidden="true"></http://december.com/html/4/element/i.html> Głos nie został dodany!.'); }); } }); });
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'"); } http://www.php.net/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) { http://www.php.net/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 = http://www.php.net/array('glos_plus'=>$vote_up, 'glos_minus'=>$vote_down); http://www.php.net/echo json_encode($send_response); break; }
Widocznie gdzieś nie kończysz drukowania danych poprzez exit.
P.S. Te SELECTy nie są w ogóle potrzebne. Wiesz jaka jest aktualnie punktacja, bo ją wyświetlasz. Wystarczy, że od tej wyświetlanej odejmiesz lub dodasz 1, a po stronie PHP zrobisz tylko UPDATE. Zmianę wartości wyświetlanej możesz zrobić dla pewności po poprawnej zwrotce z Ajax (kiedy będziesz pewien, że UPDATE się powiódł).
Ja tam nie chciałem podawać dokładnie plików i ta wersja jaka jest przedstawiona działa.
Nie chcę aby ktoś wszedł w plik js i tam widział jaki dokładnie plik odpowiada za like czy dodawanie ocen.
Oceny tą samą metodą działają ale te like coś nie pasuje. Wcześniej zamiast pliku ajax-reviews-votes.php był index.php a tam może przekombinowane ale przynajmniej wszystko przechodzi przez index.
switch ($_POST['opinia']){ // tu będzie zabezpieczenie więc luzik z nagim POST jak na razie case 'dodaj': include 'includes/ajax-reviews.php'; http_response_code(200); break; case 'glosuj': http_response_code(200); include 'includes/ajax-reviews-votes.php'; break; }
Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)