2012-09-28 84 views
1

我用喜歡/不喜歡按鈕,我的劇本找到從網上搜索這個腳本腓 - 阿賈克斯喜歡/不喜歡按鈕

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

萬物使用單一的差價,但是當工作良好,

我想要使用這個評級腳本的所有文件來自週期

例如,我有新聞劇本和所有新聞都列出,並附近的標題,你看到喜歡/不喜歡的按鈕。 (如stackoverflow網頁),我想分開評價所有這些內容。在此腳本中,我無法在同一頁面中分離文章ID

您能幫我解答嗎?

+2

肯定。你有什麼,你有什麼嘗試,什麼不適合你? –

+0

我確實成功對單個帖子進行評分,我沒有對單獨列出的一頁中的帖子進行評分。腳本只是率先發布。我想要做像stackoverflow一樣的想法。帖子和答案列在頁面中,每個人都可以通過點擊贊或不喜歡按鈕來評價每篇文章 – Seyhan

回答

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; 
} 

而且僅此而已......

+0

任何其他建議? – Seyhan

0

我有寫這個代碼,它非常適用於在PHP代碼:)使用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>