2013-04-10 21 views
0

我正在建立一個審查系統與php jquery和ajax。我已經爲「這篇評論有用嗎?」寫了下面的代碼。按鈕:'這回顧是否是有用的?'按鈕PHP jQuery的Ajax

<input type='button' value='Yes' onClick = 'myCall()' 
     style='background-color:#556B2F;color:white;padding:2px; cursor:pointer' 
     name='help' /> 
<input type='hidden' id='randomdirectory' value='$g' name='ids'/> 

$ g是我分配給存儲在數據庫中的評論ID的變量。 Ajax的腳本是這樣的:

<script> 

function myCall() { 

var ids = $('#randomdirectory').val(); 
var self = this; 

    $.ajax({ 
     url: 'rev.php', 
     type: 'POST', 

     data: {ids: ids}, 
     success: function(data) { 
     $('#ques1').hide(); 
    } 

    }); 

} 

,並在「rev.php」文件中的代碼是這樣的:

<?php 
include 'includes/db.php'; 

$q = $_POST['ids']; 
if ($q != ""){ 

$result = mysql_query("SELECT * FROM table where id='$q'"); 

while($row = mysql_fetch_array($result)) 
    { 
    $finalproduct = $row['numberofhelpfulvotes']; 
    $finalproduct1 = $finalproduct + 1; 


    } 


mysql_query("UPDATE table SET numberofhelpfulvotes='$finalproduct1' WHERE id ='$q'"); 
} 

?> 

問題是,當我在頁面上的多個評論。當您點擊任何評論中的任何「是」按鈕時,顯示的第一個評論會將投票添加到該評論中,而不是單擊該按鈕的實際評論。另外,當按鈕和文本在ajax調用後隱藏時,第一個查看按鈕和文本被隱藏,而不是點擊該按鈕的查看。頁面底部也出現一條黑線。

任何解決這些問題的解決方案將不勝感激。

+0

你有沒有給所有的按鈕不同的ID? – 2013-04-10 10:06:42

+0

沒有所有的按鈕都有#random目錄編號 – 2013-04-10 10:09:59

+0

那麼它不應該發生,你必須使用按鈕單擊事件,並且在所有按鈕的this.val()和id應該不同 – 2013-04-10 10:10:48

回答

0

感謝您的回覆。最後我做這裏面完美的作品對我來說:

<script> 

$(function() { 
    $(".button").click(function() { 
    var ids = (this.id); 

    $.ajax({ 
     url: 'rev.php', 
     type: 'POST', 

     data: {ids: ids}, 
     success: function(data) { 
     $('.' + ids).hide(); 
    } 

    }); 
    }); 
}); 


</script> 

,並以此爲HTML:

<div class='$g'><font size='2' color='black'>Was this review helpful?</font>&nbsp;<input type='submit' value='Yes' onClick = 'myCall()' style='background-color:#556B2F;color:white;padding:2px; cursor:pointer' name='help' id='$g' class='button' /><input type='hidden' id='randomdirectory' value='$g' name='ids'/> 

的PHP保持不變,但我要更新它,使之更加安全。