2013-03-01 12 views
0

每一行,我有一個PHP代碼,從數據庫回答表加載數據我的Ajax代碼犯規發送從PHP

PHP代碼:

<?php while ($rowanswer=mysql_fetch_assoc($answer)) :?> 
     <div class="text-soal2"> 

     <div class="vote-svj"> 
       <div class="vote-svj-vt3"><a class="voteupa" onclick="" ><img src="images/plus.png" alt="like" title="+"/></a></div> 
       <div class="vote-svj-vt2"><?php echo $rowanswer['vote'] ; 
       echo $aid=$rowanswer['id']; 
       ?></div> 
       <div class="vote-svj-vt"><a class="votedowna" ><img src="images/minus.png" alt="dislike" title="-"/></a></div> 

      </div> 


     <div class="text-javab"> 


       <p> 

      <?php echo $rowanswer['answer'] ?> 



       </p> 


     </div><div class="clr"></div> 
     <h5>نوشته شده توسط <?php 

     $query1="select username from user where id=$rowanswer[user_id]"; 
     $senderanswer=mysql_query($query1); 
$ssender=mysql_fetch_assoc($senderanswer); 

     echo $ssender['username']; 

     ?> در تاریخ <?php echo $rowanswer['date'] ?></h5> 
    </div> 
     <?php endwhile; ?> 

我努力送$援助= $ rowanswer [」 ID']答案ID與阿賈克斯到另一個頁面 但它只是最後的援助發送到另一個網頁我怎麼能每一行援助發送到與阿賈克斯,當我在它的按鈕

Ajax代碼,請點擊:

$('.votedowna').click(function(){ 
$.ajax({ 
     url : '<?php echo "php_func/answer_vote.php?aid=".$aid ?>', 
     type : 'GET', 

     data : { send : "dislike"}, 
     beforeSend : function() 
     { 
      alert(<?php echo $aid ?>); 
      }, 
     success : function(callback){ 
      alert(callback); 

     }) 


    }); 
+0

是什麼試圖做什麼?解釋,我們會幫助你。 – Othman 2013-03-01 07:24:17

+0

爲什麼不在數組中保存$ rowanswer ['id']值(使$ aid數組),而不是將其放到常規變量中?如果使用普通變量,則每次都覆蓋它,並且最後只能保留最後一次。 – 2013-03-01 07:26:46

回答

2

你可以試試這個:

 
<div class="vote-svj-vt"><a class="votedowna" id="<?php echo $rowanswer['id'];"><img src="images/minus.png" alt="dislike" title="-"/></a></div> 

然後在JS就去拿,我們在votedowna錨標記補充說,ID和URL傳遞。

 
$('.votedowna').click(function(){ 
var aid = $(this).attr("id"); 
$.ajax({ 
     url : 'php_func/answer_vote.php?aid='+aid, 
     type : 'GET', 

     data : { send : "dislike"}, 
     beforeSend : function() 
     { 
      alert(); 
      }, 
     success : function(callback){ 
      alert(callback); 

     }) 


    }); 
+0

謝謝你的代碼,但是我能爲投票做些什麼? 如果我使用它的ID將衝突togeter ... – pencilvania 2013-03-01 07:34:56

+0

該ID應該是唯一的,只要投票的關注,就在你看到我的答案3票的情況下,你可以點擊上面的箭頭3,也接受我的答案如果它幫助你。 – 2013-03-01 07:38:02

1

因爲你.votedowna不斷重複這取決於您while循環具有迭代的量,你必須之前有一個.each語句要如何處理click事件。

$('.votedowna').each(function() { 
$(this).click(function() { 
    $.ajax(... 

這可能是問題所在。 順便說一下,您是否在PHP中使用while創建了Javascript代碼,或者您是否有.js文件? 因爲如果你使用JS文件,那麼你必須以不同的方式通過AJAX網址,這意味着你的Javascript應該是這樣的,

$.ajax({ 
    url : 'php_func/answer_vote.php', 
    type : 'GET', 

    data : { 
     send : "dislike", 
     aid: $(this).parent().parent().find('div.vote-svj-vt2').text(), // This is where you have the aid for this row, you better save it somewhere else though 
    }, 
    beforeSend : function() 
    { 
     alert('<?php echo $aid ?>'); 
     }, 
    success : function(callback){ 
     alert(callback); 

    }) 
0

首先,如果你試圖調用一個Ajax方法很多次了,你可以使用foreachfor循環來做到這一點。

其次,請不要在腳本中使用mysql_query,因爲它太舊了。改爲使用PDO

最後,如果你需要選擇在JavaScript任何你可以多次使用。每()而不是使用。點擊

此外,使用標準查詢寫作方法這樣

SELECT username FROM user WHERE id= '$rowanswer[user_id]'