0

我有一个从数据库答案表中加载数据的 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; ?> 

我努力将带有 ajax 的 $aid=$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);

        })


    });
4

3 回答 3

2

你可以试试这个:

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

然后在 JS 中只需获取我们在锚标记中添加的那个 idvotedowna并用 url 传递它。

$('.votedowna').click(function(){
varaid = $(this).attr("id");
$.ajax({
         网址:'php_func/answer_vote.php?aid='+aid,
         类型:'GET',

         数据:{发送:“不喜欢”},
        发送前:函数()
        {
            警报();
            },
        成功:函数(回调){
            警报(回调);

        })


    });
于 2013-03-01T07:26:36.023 回答
1

因为您的.votedowna重复次数取决于循环的迭代次数,所以您必须在要如何处理事件之前while有一个语句。.eachclick

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

这可能是问题所在。顺便问一下,您是否while在 PHP 中创建了 Javascript 代码,或者您有.js文件吗?因为如果您使用 JS 文件,那么您必须以不同的方式传递 ajax URL,这意味着您的 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);

    })
于 2013-03-01T07:27:51.480 回答
0

首先,如果你想多次调用一个ajax方法,你可以使用foreach或者for循环来做。

其次,请不要mysql_query在你的脚本中使用,因为它太旧了。改用PDO

最后,如果您需要在 javascript 中多次选择任何内容,您可以使用 .each() 而不是使用 .click

另外,使用像这样的标准查询编写方法

 SELECT username FROM user WHERE id= '$rowanswer[user_id]'
于 2013-03-01T07:33:31.827 回答