首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在javascript中使用来自php的while循环进行onclick

在javascript中使用来自php的while循环进行onclick
EN

Stack Overflow用户
提问于 2019-04-25 03:01:44
回答 1查看 162关注 0票数 0

几天来,我一直在拼命地向评论区添加回复字段--基本上,我希望只要你点击某个按钮,回复字段(Div)就会出现。我认为使用onclick事件处理程序是可行的。

我们已经在一个php回显中了,所以我认为只需将row-id (cid)添加到实际的div和button id中会很容易-事实证明它不起作用。

将myFunction()添加到按钮

添加了"myDIV“来检查代码是否可以与这个简单的div一起工作-它确实可以

添加了ID以打印出div和按钮的名称,同时添加了" cid“以在浏览器中获得更好的概述( brwoser中的cid正如预期的那样)-最后,我摆脱了代码,只是提醒了cid,并且非常惊讶地发现它是一个我没有预料到的数字。它是数据库中的最后一个cid。

代码语言:javascript
复制
  if($usermatchcom1 = $usermatchcom->fetch_assoc()){

    echo "<div class='comment_box'><p>";
    echo $usermatchcom1['uidusers']."<br>";
    echo $comments1['date']."<br>";
    echo nl2br($comments1['message']);
    echo "</p>";

    if(isset($_SESSION['userid'])){
      if($_SESSION['userid'] == $usermatchcom1['idusers']){
        echo "<form class='delete_form' method='POST' action='".deletecomments($conn)."'>
        <input type='hidden' name='cid' value='".$comments1['cid']."'>
        <input type='hidden' name='sid' value='$sid'>
        <input type='hidden' name='sname' value='$sname'>
        <button type='submit' name='deletesubmit'>Delete</button>
        </form>
        <form class='edit_form' method='POST' action='editcomment.php'>
          <input type='hidden' name='cid' value='".$comments1['cid']."'>
          <input type='hidden' name='uidcomments' value='".$comments1['uidcomments']."'>
          <input type='hidden' name='uidname' value='".$comments1['uidusers']."'>              
          <input type='hidden' name='date' value='".$comments1['date']."'>
          <input type='hidden' name='message' value='".$comments1['message']."'>
          <input type='hidden' name='sid' value='$sid'>
          <input type='hidden' name='sname' value='$sname'>
          <button>Edit</button>
        </form>";
      }else{
          echo "<form class='reply_form' method='POST' action='replycomment.php'>

          <input type='hidden' name='uidcomments' value='".$comments1['uidcomments']."'>
          <input type='hidden' name='date' value='".$comments1['date']."'>
          <input type='hidden' name='cid' value='".$comments1['cid']."'>
          <input type='hidden' name='sid' value='$sid'>
          <input type='hidden' name='sname' value='$sname'>
          <input type='hidden' name='message' value='".$comments1['message']."'>
          <button>Reply</button>
          </form>";
      }
    }else{
      echo "<p class='replymessage'>You need to be logged in to reply</p>";
    }

    echo "</div>";

      }

      foreach($replymatch as $replymatch1){

        echo "<div class='reply_box'><p>";
        echo $replymatch1['uidusers']."<br>";
        echo $replymatch1['date']."<br>";
        echo nl2br($replymatch1['message']);
        echo "</p><br>";



        if(isset($_SESSION['userid'])){
          if($_SESSION['userid'] == $replymatch1['uidcomments']){
        echo "<form class='delete_form' method='POST' action='".deletecomments($conn)."'>
        <input type='hidden' name='cid' value='".$comments1['cid']."'>
        <input type='hidden' name='sid' value='$sid'>
        <input type='hidden' name='sname' value='$sname'>
        <button type='submit' name='deletesubmit'>Delete</button>
        </form>
        <form class='edit_form' method='POST' action='editcomment.php'>
          <input type='hidden' name='cid' value='".$comments1['cid']."'>
          <input type='hidden' name='uidcomments' value='".$comments1['uidcomments']."'>
          <input type='hidden' name='date' value='".$comments1['date']."'>
          <input type='hidden' name='message' value='".$comments1['message']."'>
          <input type='hidden' name='sid' value='$sid'>
          <input type='hidden' name='sname' value='$sname'>
          <button>Edit</button>
        </form>";
      }else{
          echo "<form class='reply_form' method='POST' action='replycomment.php'>

          <input type='hidden' name='uidcomments' value='".$comments1['uidcomments']."'>
          <input type='hidden' name='date' value='".$comments1['date']."'>
          <input type='hidden' name='cid' value='".$comments1['cid']."'>
          <input type='hidden' name='sid' value='$sid'>
          <input type='hidden' name='sname' value='$sname'>
          <input type='hidden' name='message' value='".$comments1['message']."'>
          <button>Reply</button>
          </form>
          <button id='test_".$replymatch1['cid']."' onclick='myFunction()'>Test</button>";
      }
    }else{
      echo "<p class='replymessage'>You need to be logged in to reply</p>";
    }

    echo "</div>";

    echo "

    <style>
    .replytest_".$replymatch1['cid']."{
      width: 400px;
      margin-left: 50px;
      max-height: 200px;
      overflow: hidden;
      background: #fff;
      color: black;  
    }
    .replytest_".$replymatch1['cid'].".open{
      max-height: 80px;
      color: red;

    }

    #myDIV {
      width: 400px;
      margin-left: 50px;
      max-height: 200px;
      overflow: hidden;
      background: #fff;
      color: black;  
    }

    </style>";

    echo "

<div id='myDIV'>
This is my DIV element.
</div>
<br>

代码语言:javascript
复制
<p id='testtext'>Divs name is replytest_".$replymatch1['cid']." and buttons name is showmore_".$replymatch1['cid']."</p><br>

<p id='showme'>Test</p>

</div>       
<script>
  function myFunction() {
  var x = document.getElementById('replytest_".$replymatch1['cid']."');
    if (x.style.display === 'none') {
      x.style.display = 'block';
    } else {
     x.style.display = 'none';
    }
   }
代码语言:javascript
复制
 ";

我“简单地”想要一个文本区出现在我点击“回复”的评论下面-据我所知,我必须在php while循环中做这件事,因为我从数据库收到评论。

任何帮助都将不胜感激

Thx的家伙

EN

回答 1

Stack Overflow用户

发布于 2019-04-25 03:33:47

这是您使用jquery所需的解决方案。现在你可以在这里使用php来处理动态数据了。

代码语言:javascript
复制
$(document).ready(function(){
  $("button").click(function(){
    $("#replyField").fadeToggle();
  });
});
代码语言:javascript
复制
button{
 display:block;
 }
 
#replyField {
 
 display:none
 }
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button>Reply</button>
<textarea rows="4" cols="50" id="replyField">
 
</textarea>

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55836906

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档