Php - Ajax喜欢/不喜欢按钮

时间:2012-09-28 13:16:45

标签: php ajax social-media-like

我通过使用喜欢/不喜欢按钮为我的脚本

从互联网搜索中找到了这个脚本

http://wcetdesigns.com/view.php?dtype=tutorials&category=php&id=22

当使用单个帖子时,Everythings工作得很好,但

我想使用此评级脚本来处理来自周期的所有文件

例如,我有新闻脚本,所有新闻都列出来了,你看到喜欢/不喜欢按钮的标题附近。 (像stackoverflow网页),我想分别评价所有这些内容。在这个脚本中,我无法在同一页面中分隔文章ID

你能帮我解决一下吗?

2 个答案:

答案 0 :(得分:7)

首先,您需要为每个按钮指定哪个文章适用,您可以使用数据属性轻松完成

<button class="votebutton" data-article="1" data-vote="1">Up vote</button>
<button class="votebutton" data-article="1" data-vote="-1">Down vote</button>
<span class="votes" data-article="1"><?=//print the current number of votes of this article//?>

现在,您需要在单击按钮时执行某些操作,因此您的javascript看起来应该是这样的:

$('.votebutton').on("click", function(){
  vote = $(this).attr("data-vote");
  article = $(this).attr("data-article");
  rateArticle(vote, article);
})

现在你需要rateArticle()函数,它看起来像这样:

function rateArticle(vote, article){ 
  var data = 'vote='+rating+'&articleID='+article;
  $.ajax({
     type: 'POST',
     url: 'rateArticle.php', 
     data: data,
     success: function(votes){
         $('.vote[data-article='+article+']').html(votes); 
     }
   });
}

在服务器端,你发布的rateArticle.php文件看起来像这样:

$vote = $_POST['vote'];
$articleID = $_POST['articleID'];
$currentVotes = ; //fill it with the votes that the article currently have

if($vote != "-1" || vote != "1"){
  //goddamn script kids messing with us again, let's put them back where they belong
  header("Location: http://www.lemonparty.com"); //probably not this, but you can think of something
}

if(userHasAlreadyVoted()){ //if user has already voted we just return the current number of votes
  echo $currentVotes;
}else{
  $newVotes = $currentVotes + $vote;
  //save newVotes to db...
  echo $newVotes;
}

就是这个......

答案 1 :(得分:0)

我已经编写了这段代码,并且在p.h.p代码中使用ajax时它非常适合喜欢:)

我把它分为三个页面,名为test1.php,test1.js,test1php.php

test1.php

<?php
    mysql_connect("localhost","root","");
    mysql_select_db("school");
?>
<html>
    <head>
        <script type="text/javascript" src="test1.js"></script>
    </head>
    <body>
        <form action="" method="post" enctype="">
            <table id="status_id">
            <?php
                $qry = mysql_query("SELECT * from s_user");
                while($row = mysql_fetch_array($qry)){
                    $uid = $row['u_id'];
                    $status = $row['u_status'];
                    $uname = $row['u_uname'] . "<br />";
            ?>
                <tr>
                    <td><?php echo $uname; ?></td>
                    <td><div id=""><?php echo $status; ?></div></td>
                    <td><input type="button" onclick="return change_status(this.value);" value="<?php echo $uid; ?>"></td>
                <?php } ?>
                </tr>
            </table>                    
        </form>
    </body>
</html> 

test1.js

function change_status(country)
{
  // alert("hellow");
if (country.length==0)
  {
   //alert("hellow"); 
  document.getElementById("status_id").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("status_id").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","test1php.php?status_id="+country,true);
xmlhttp.send();
}

test1php.php

    <?php
    mysql_connect("localhost","root","");
    mysql_select_db("school");
    $id = $_GET['status_id'];
    $q = mysql_query("SELECT * from s_user WHERE u_id = $id");
    $r = mysql_fetch_array($q);
    $fetch_status = $r['u_status'];
    if($fetch_status == 1){
        mysql_query("UPDATE s_user SET u_status = 0 WHERE u_id = '$id'");
    }else{
        mysql_query("UPDATE s_user SET u_status = 1 WHERE u_id = '$id'");
    }
?>
            <table id="status_id">
            <?php
                $qry = mysql_query("SELECT * from s_user");
                while($row = mysql_fetch_array($qry)){
                    $uid = $row['u_id'];
                    $status = $row['u_status'];
                    $uname = $row['u_uname'] . "<br />";
            ?>
                <tr>
                    <td><?php echo $uname; ?></td>
                    <td><div id=""><?php echo $status; ?></div></td>
                    <td><input type="button" onclick="return change_status(this.value);" value="<?php echo $uid; ?>"></td>
                <?php } ?>
                </tr>
            </table>