ajax中的php变量不';t变化值

ajax中的php变量不';t变化值,php,ajax,post,Php,Ajax,Post,我在PHP文件的while循环中有一个按钮。 该按钮与一个PHP值相关联,该值随mysqli\u fetch\u数组而变化。 我在我的mysqli\u fetch\u数组中只加载了3个值。 我的ajax函数中有数据,我的问题是它只接受第一行的值,而不接受其他行的值。事实上,函数在mysqli_fetch_数组的第一行就可以了,而我的代码似乎没有在其他行上运行,就像PHP循环在第一行停止一样 这是我的javscript代码(在while之后,它在我的循环的同一页上声明): $(文档).ready

我在PHP文件的while循环中有一个按钮。 该按钮与一个PHP值相关联,该值随
mysqli\u fetch\u数组
而变化。 我在我的
mysqli\u fetch\u数组中只加载了3个值。
我的ajax函数中有数据,我的问题是它只接受第一行的值,而不接受其他行的值。事实上,函数在mysqli_fetch_数组的第一行就可以了,而我的代码似乎没有在其他行上运行,就像PHP循环在第一行停止一样

这是我的javscript代码(在while之后,它在我的循环的同一页上声明):


$(文档).ready(函数(){
$(“#向上”)。单击(函数(){
var varno_commentaire=$(“#no_commentaire”).val();
$.ajax({
url:“upvoteCommentaire.php”,
类型:“POST”,
数据:{
无注释:无注释
},
async:false,
成功:功能(结果){
警报(结果);
}
});
});
});
下面是我的PHP循环:

<?php 
while($comm = mysqli_fetch_array($res2))
{
    echo
        '<label class="text-muted">'.$comm['desc_commentaire'].'</label>'.
            '<p>'.
        '<a href="">'.'@'.$comm['login'].'</a>'.
            '<p>'.
        '<label>'.$comm['vote_commentaire'].' points'.'</label>'.
            '<p>'.
        '<input type="text" value="'.$comm['no_commentaire'].'" id="no_commentaire"/>'.
        '<button class="btn btn-default" name="up" id="up">+</button>'.
            '&nbsp'.
        '<button class="btn btn-default" name="down" id="bold" onclick="downvote()">-</button>'.
            '<p>'.
        '<hr/>';
}
?>


我希望它会很容易理解。任何帮助都将不胜感激,我已经坚持了一会儿。非常感谢。

首先,您不能多次声明
id
。ID应该是唯一的。您可以将ID传递到HTML属性中

您的循环:

<?php 
while($comm = mysqli_fetch_array($res2))
{
    echo
        '<label class="text-muted">'.$comm['desc_commentaire'].'</label>'.
            '<p>'.
        '<a href="">'.'@'.$comm['login'].'</a>'.
            '<p>'.
        '<label>'.$comm['vote_commentaire'].' points'.'</label>'.
            '<p>'.
        '<input type="text" value="'.$comm['no_commentaire'].'"/>'.
        '<button class="btn btn-default btn-up" data-id="'.$comm['no_commentaire'].'">+</button>'.
            '&nbsp'.
        '<button class="btn btn-default btn-down" data-id="'.$comm['no_commentaire'].'">-</button>'.
            '<p>'.
        '<hr/>';
}
?>

您的脚本:

<script type="text/javascript">
  $(document).ready(function() {
    $("button.btn-up").click(function() {
      var varno_commentaire = $(this).data('id');

      $.ajax({
        url: "upvoteCommentaire.php",
        type: "POST",
        data: {
          no_commentaire: varno_commentaire
        },
        async: false,
        success: function(result) {
          alert(result);
        }
      });
    });
  });

</script>

$(文档).ready(函数(){
$(“button.btn up”)。单击(函数(){
var varno_commentaire=$(this).data('id');
$.ajax({
url:“upvoteCommentaire.php”,
类型:“POST”,
数据:{
无注释:无注释
},
async:false,
成功:功能(结果){
警报(结果);
}
});
});
});

ID
仅在整个页面上使用一次。在您的例子中,PHP的
while
循环中的每个元素都具有相同的ID。因此,当您通过
$(“#no_commentaire”).val()获取元素的值时,它将获取第一个元素的值,而忽略页面上的其余元素

您的问题的解决方案是使用
而不是元素的
ID
,并通过jQuery中的
父项
子项
关系访问相同的元素。因此,您只能获得selected/clicked节的值

你的PHP代码看起来像这样

<?php 
while($comm = mysqli_fetch_array($res2))
{
    echo
        '<div class="parent-class">'.
        '<label class="text-muted">'.$comm['desc_commentaire'].'</label>'.
            '<p>'.
        '<a href="">'.'@'.$comm['login'].'</a>'.
            '<p>'.
        '<label>'.$comm['vote_commentaire'].' points'.'</label>'.
            '<p>'.
        '<input type="text" value="'.$comm['no_commentaire'].'" class="no_commentaire"/>'.
        '<button class="btn btn-default up" name="up">+</button>'.
            '&nbsp'.
        '<button class="btn btn-default" name="down" id="bold" onclick="downvote()">-</button>'.
            '<p>'.
        '</div>'.
        '<hr/>';
}
?>
<script type="text/javascript">
  $(document).ready(function() {
    $(".up").click(function() {
      var varno_commentaire = $(this).parent().child(".no_commentaire").val();

      $.ajax({
        url: "upvoteCommentaire.php",
        type: "POST",
        data: {
          no_commentaire: varno_commentaire
        },
        async: false,
        success: function(result) {
          alert(result);
        }
      });
    });
  });

</script>

jQuery代码看起来像这样

<?php 
while($comm = mysqli_fetch_array($res2))
{
    echo
        '<div class="parent-class">'.
        '<label class="text-muted">'.$comm['desc_commentaire'].'</label>'.
            '<p>'.
        '<a href="">'.'@'.$comm['login'].'</a>'.
            '<p>'.
        '<label>'.$comm['vote_commentaire'].' points'.'</label>'.
            '<p>'.
        '<input type="text" value="'.$comm['no_commentaire'].'" class="no_commentaire"/>'.
        '<button class="btn btn-default up" name="up">+</button>'.
            '&nbsp'.
        '<button class="btn btn-default" name="down" id="bold" onclick="downvote()">-</button>'.
            '<p>'.
        '</div>'.
        '<hr/>';
}
?>
<script type="text/javascript">
  $(document).ready(function() {
    $(".up").click(function() {
      var varno_commentaire = $(this).parent().child(".no_commentaire").val();

      $.ajax({
        url: "upvoteCommentaire.php",
        type: "POST",
        data: {
          no_commentaire: varno_commentaire
        },
        async: false,
        success: function(result) {
          alert(result);
        }
      });
    });
  });

</script>

$(文档).ready(函数(){
$(“.up”)。单击(函数(){
var varno_commentaire=$(this.parent().child(“.no_commentaire”).val();
$.ajax({
url:“upvoteCommentaire.php”,
类型:“POST”,
数据:{
无注释:无注释
},
async:false,
成功:功能(结果){
警报(结果);
}
});
});
});

id
对于不同的按钮应该是不同的。。。多个元素不应该有相同的idYeah,获取值的唯一方法是使用
$(“#no_commentaire”).val()
–当您的页面中有多个
#no_commentaire
元素时,该如何工作?我认为您是对的,很抱歉问一下,你对什么是好的抓取功能有什么建议吗?我删除了我关于使用错误的抓取功能的评论,我可能错了。请看下面的答案。谢谢大家的回答,我将投资于此,解决后,我将标记正确的一个,并告诉你更多!您的“向上”按钮有两个
;)您的方法有效,我只需将数据id值更改为:data id=“.”.$comm['no_commentaire']”。谢谢您的帮助,先生!