简体   繁体   English

如何用PHP实现Jquery-upvote插件

[英]How to implement Jquery-upvote plugin with php

I have a jQuery plugin up vote in my project. 我在我的项目中对jQuery插件投了赞成票 I want to have a up and down vote functionality like stack overflow but I am total confused on how to pass values through the above up vote plugin function to my php file. 我想拥有向上和向下投票功能,例如堆栈溢出,但是我对如何通过上面的向上投票插件功能将值传递给我的php文件感到困惑。 I have tried to pass my id param in the Ajax but I am only getting the id of the first row. 我试图在Ajax中传递id参数,但是我只得到第一行的id。 I used to use on-click event handler and so that I can track which row is been clicked but I can't implement that method with this function. 我曾经使用过单击事件处理程序,因此可以跟踪单击了哪一行,但是无法使用此功能实现该方法。 Or someone can explain how do I implement the above jQuery plugin with php on my page. 或者有人可以在我的页面上说明如何使用php实现上述jQuery插件。

My HTML in php for loop: 我在php中的HTML for循环:

  $sample = $this->db->prepare("SELECT * from Ratings");
  $sample->execute();
  $RES = $sample->fetchAll(PDO::FETCH_ASSOC); 
  foreach ($RES as $data){

      $upVote =  $data['Upvote'];
      $downvote =  $data['Downvote'];
      $counter = $data['Counter'];

echo '<tr>

        <td>
        <div value='.$id.' name="id" class="upvote upvoteForm upvote-serverfault">
         <a class="upvote">'.$upVote.'</a>
                <span class="count">'.$counter.'</span>
                 <a class="downvote">'.$downvote.'</a>
                 <a class="star"></a>
       </td></tr>';
    }

My jquery function : 我的jQuery函数:

<script>
var callback = function(element) {
    $.post(
     "voting.php/",
     {
         id: $(".id").val(),
         upvoted: $(".upvote").val(),
         downvoted:  $(".downvote").val(),
         star: $(".star").val()
     },
   );
};
$('.upvoteForm').upvote({callback});

</script>

Check out the Initialization examples in the doc . 查看doc中初始化示例 the callback method receives an object, which holds the data of the currently voted element. 回调方法接收一个对象,该对象保存当前已投票元素的数据。 Don't try to retrieve the values with jQuery selectors, use the element parameter. 不要尝试使用jQuery选择器来检索值,请使用element参数。 And if I understand the documentation correctly, you can even omit the data object completely if you keep the DOM structure and add a data-id attribute: 如果我正确理解了文档,那么如果保留DOM结构并添加data-id属性,甚至可以完全省略数据对象:

<div data-id="'.$id.'" class="upvote upvoteForm upvote-serverfault">

var callback = function(element) {
    $.post(
         "voting.php/",
         // this object could probably be omitted
         {
             id: element.id,
             upvoted: element.upvoted,
             downvoted:  element.downvoted,
             star: element.star
         },
    );
};
$('.upvoteForm').upvote({callback: callback});

Note: I have not tested this, no guarantee that it works :-) 注意:我尚未对此进行测试,因此不能保证它会起作用:-)

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

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