简体   繁体   English

用jquery更新点赞总数

[英]Update total amount of likes with jquery

I have created a like button for my page using jquery, html and php. 我已经使用jquery,html和php为页面创建了一个赞按钮。 And now I'm trying to read out the total amount of likes. 现在,我正在尝试读出喜欢的总数。

When I click on the it goes through my jquery and php and returns the total amount in my console. 当我单击时,它将遍历我的jquery和php,并在控制台中返回总金额。 But I can't see it on the page until I update the page. 但是直到更新页面我才能在页面上看到它。

PHP and HTML PHP和HTML

 <?php


$page = new CMS();
$gp = $page->getPage();


foreach ($gp as $sp) {
  //var_dump($sp);

  echo "<div class='pub'>";

  echo "<h4 class='pub-headline'>" . $sp['title'] . "</h4>"; 
  echo "<article class='pub_art'>" . $sp['content'] . "</article>";  
  echo "<p class='pub_created'>" . $sp['created'] . "</p>"; 
  echo "<p class='pub_created_by'>". $sp['writer'] ."</p>";

  echo "<button class='show'>Show</button>"; 
  echo "<button class='noshow'>Hide</button>";

  echo "<div class='vote_widget'>";  
  echo "<div class='voting' onclick='vote(" . $sp['id'] . ", 1)'></div>";

  echo"<div class='total_likes'>" . $sp['likes'] . "</div>";

  echo"</div>";

  echo "</div>";

 }
?>

Jquery jQuery的

function vote(id, likes) {


    $.post("classCalling4.php", 
            { id: id, likes: likes }, function(result){

                console.log(result)
                $(".total_likes" + id) .html(result);
                ;
    });

OOP PHP 面向对象的PHP

    public function updateLikes($id, $likes) {

    $id = mysqli_real_escape_string($this->db, $id);
    $likes = mysqli_real_escape_string($this->db, $likes);

    $id = intval($id);
    $likes = intval($likes);


    $sql = "UPDATE pages
    SET likes = likes+1
    WHERE id = $id ";

    $result = mysqli_query($this->db, $sql) or die("Fel vid SQL query 1"); // Hit kommer jag


    $sql2 = "SELECT * from pages WHERE id = $id ";
    $result2 = mysqli_query($this->db, $sql2) or die ("Fel vid SQL query 2");


    $row = mysqli_fetch_array($result2);
    $tot_likes = $row['likes']; 

    echo $tot_likes;

}

you should add the id to class name because you suppose this in your jquery script: 您应该将id添加到类名,因为您在jquery脚本中假设了这个:

$(".total_likes" + id) .html(result);

so your html should be: 所以您的html应该是:

echo"<div class='total_likes".$sp['id']."'>" . $sp['likes'] . "</div>";

and if you use this class to give style you can replace the class with id: 如果您使用此类提供样式,则可以使用id替换该类:

echo"<div class='total_likes' class='total_likes".$sp['id']."'>".$sp['likes']."</div>";

and your javascript should be: 并且您的JavaScript应该是:

$("#total_likes" + id) .html(result);

Since, the total_likes is in foreach loop so you should concate id with the class name to make it unique. 由于total_likesforeach循环中,因此您应将id与类名连接起来以使其唯一。 So, instead of 所以,代替

  echo"<div class='total_likes'>" . $sp['likes'] . "</div>";

do this 做这个

 echo"<div class='total_likes'".$sp['id'].">" . $sp['likes'] . "</div>";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM