【问题标题】:php variable in ajax doesn't change valueajax中的php变量不会改变值
【发布时间】:2017-07-25 11:21:17
【问题描述】:

我有一个按钮进入 PHP 文件的 while 循环。 该按钮与随mysqli_fetch_array 更改的PHP 值相关联。 我在mysqli_fetch_array 中只加载了 3 个值。 我的 ajax 函数中有数据,我的问题是它只接受第一行的值,而不接受其他行的值。事实上,函数在mysqli_fetch_array 的第一行就可以了,而且我的代码似乎在其他行上不起作用,就像 PHP 循环在第一行停止一样。

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

<script type="text/javascript">
  $(document).ready(function() {
    $("#up").click(function() {
      var varno_commentaire = $("#no_commentaire").val();

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

</script>

这是我的 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
  • 是的,您获得价值的唯一方法是使用 $("#no_commentaire").val() - 当您的页面中有多个 #no_commentaire 元素时,它应该如何工作?
  • 我认为你是对的,很抱歉问这个问题,但你对什么是好的获取功能有什么建议吗?
  • 我删除了关于使用错误获取功能的评论,我可能错了。请参阅下面的答案。
  • 嗯,谢谢大家的回答,我会投资,解决后我会标记正确的并告诉你更多!

标签: php ajax post


【解决方案1】:

首先,您不能多次声明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>

【讨论】:

  • 您的方法有效,我只需将 data-id 值更改为:data-id="'.$comm['no_commentaire'].'"
  • 感谢您的帮助,先生!
【解决方案2】:

ID 在整个页面上只能使用一次。在您的情况下,PHP 的 while 循环中的每个元素都具有相同的 ID。因此,当您通过$("#no_commentaire").val() 获取元素的值时,它会获取第一个元素的值,而忽略页面上的其余元素。

您的问题的解决方案是使用 class 而不是 ID 的元素,并通过 jQuery 中的 parent child 关系访问相同的元素。所以你只能得到选定/点击部分的值。

你的 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/>';
}
?>

jQuery 代码看起来像这样,

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

【讨论】:

  • 你的向上按钮有两个class ;)
猜你喜欢
  • 2017-02-13
  • 1970-01-01
  • 1970-01-01
  • 2014-01-01
  • 1970-01-01
  • 2016-12-23
  • 1970-01-01
  • 2015-06-17
  • 1970-01-01
相关资源
最近更新 更多