如何显示带有id的评论回复框

时间:2014-11-24 15:02:21

标签: javascript php jquery

我的回复仅适用于最后/新评论。如果我回复其他人评论它只在文本区域显示空白。

测试用例:如果我在wall.php页面的结尾处替换回复表格,请应用css display:block以显示它,单击父ID的回复按钮并在写完任何文本后提交然后它可以工作但点击回复按钮无法打开它自己的回复表单。

如果我在页面末尾替换它,如何打开我的回复表单?

wall.php(当前案例:如上所述链接,不保存旧评论的任何textarea数据)

script type='text/javascript'>
  $(function(){
    $("a.reply").click(function() {
    var id = $(this).attr("id");
    $("#parent_id").attr("value", id);
    });
  });
</script>

<script>
$('a.reply').livequery("click",function() {
   var ID = $(this).attr('id').replace('','');
   $("#reply_here-"+ID).slideToggle('slow');
   return false;
}); 
</script>
</head>


global $dbh;
$results = mysqli_query($dbh,"SELECT * FROM comments_lite WHERE qazi_id='$tutid' ORDER BY id DESC LIMIT 5") or die(mysqli_error($dbh));
//find the number of comments
list($commentNum) = mysqli_fetch_array(mysqli_query($dbh,"SELECT COUNT(*) FROM comments_lite WHERE qazi_id='$tutid'"));

echo'<div class="comments"><div id="updates"><div class="content"><comment>';

while($rows = mysqli_fetch_array($results)) 
{
$id = $rows['id'];
$qazi_id = $rows['qazi_id'];
$likes = $rows['likes'];
$username = $rows['username'];
$img = $rows['img'];
$description = $rows['description'];        
$url = $rows['url'];
$parent_id = $rows['parent_id'];
$date = $rows['date'];

    echo'<div class="comment">
        <div class="cdomment_cheder">';
        if ($url=="") {
        echo'<p class="name">'.$username.' Says:</p>'; }
        else  echo'<p class="name"><a href="userinfo.php?user='.$url.'" title="'.$username.'">'.$username.'</a> Says:</p>';
        echo'<span class="cdomment_time">'.$date.'</span><br/>
        <div class="avatarcnt">
        <img alt="" src="uploadprofile/'.$u_imgurl.'"/>
        </div></div></div>
        <div class="cdomment_text">';
        if ($description=="") {echo '';}
        else echo''.htmlentities($description).'<br>';
        echo '</div>';


//reply
echo'<div class="reply_box"><a href="javascript: void(0)" class="reply" id="'.$id.'">Reply</a></div>';

//Likes
echo'<div></div>'; 

// Only Reply 
$query = "SELECT * FROM comments_reply WHERE parent_id ='".$id."' ORDER BY id DESC";
$res = mysqli_query($dbh,$query);
while($row = mysqli_fetch_array($res)){   
$id = $row['id'];
$qazi_id = $row['qazi_id'];
$likes = $row['likes'];
$username = $row['username'];
$img = $row['img'];
$description = $row['description'];     
$url = $row['url'];
$parent_id = $row['parent_id'];
$date = $row['date'];

        echo' <div class="rcontent"><replycomment><ul>
        <div class="comment">
        <div class="cdomment_cheder">';
        if ($url=="") {
        echo'<p class="name">'.$username.' Says:</p>'; }
        else  echo'<p class="name"><a href="userinfo.php?user='.$url.'" title="'.$username.'">'.$username.'</a> Says:</p>';
        echo'<span class="cdomment_time">'.$date.'</span><br/>
        <div class="avatarcnt">
        <img alt="" src="uploadprofile/'.$u_imgurl.'"/>
        </div></div></div>
        <div class="cdomment_text">';
        if ($description=="") {echo '';}
        else echo''.htmlentities($description).'<br>';
        echo '</div>';

        </ul><replycomment></div>'; 

}   //reply close   
?>

// REPLY FORM
<div id="loader"></div><div class="reply_here" id="reply_here-<?php echo $rows['id'] ?>" style="display:none;">
    <form action="" method="post" id="repfrm">
      <fieldset id="cmntfs">
        <legend class="pyct">Your comment</legend>
        <input type="hidden" name="username" id="author" value="<?php echo $_SESSION['username'] ?>"/>
        <input type="hidden" name="url" id="url" value="" />
        <div id="maintbox">
        <textarea name="replycom" rows="2" id="replycom"></textarea>
        </div>
        <input type="hidden" name="parent_id" id="parent_id" value="0" />
        <input type="hidden" name="tutid" id="tutid" value="<?php echo $tutid ?>" />
        <button type="submit" name="submit" value="" id="submit" class="repfrm">Post Reply</button>
      </fieldset>
    </form>
</div>
<?
}    //comment close

echo'</div><comment></div>';

//pagination 

wall.php(tast case:当表单放在页面末尾时,这不会打开每个回复表单)

script type='text/javascript'>
$(function(){
 $("a.reply").click(function() {
  var id = $(this).attr("id");
 $("#parent_id").attr("value", id);
 });
});
</script>

<script>
$('a.reply').livequery("click",function() {
 var ID = $(this).attr('id').replace('','');
  $("#reply_here-"+ID).slideToggle('slow');
  return false;
}); 
</script>
</head>


global $dbh;
$results = mysqli_query($dbh,"SELECT * FROM comments_lite WHERE qazi_id='$tutid' ORDER BY id DESC LIMIT 5") or die(mysqli_error($dbh));
//find the number of comments
list($commentNum) = mysqli_fetch_array(mysqli_query($dbh,"SELECT COUNT(*) FROM comments_lite WHERE qazi_id='$tutid'"));

echo'<div class="comments"><div id="updates"><div class="content"><comment>';

while($rows = mysqli_fetch_array($results)) {
$id = $rows['id'];
$qazi_id = $rows['qazi_id'];
$likes = $rows['likes'];
$username = $rows['username'];
$img = $rows['img'];
$description = $rows['description'];        
$url = $rows['url'];
$parent_id = $rows['parent_id'];
$date = $rows['date'];

    echo'<div class="comment">
        <div class="cdomment_cheder">';
        if ($url=="") {
        echo'<p class="name">'.$username.' Says:</p>'; }
        else  echo'<p class="name"><a href="userinfo.php?user='.$url.'" title="'.$username.'">'.$username.'</a> Says:</p>';
        echo'<span class="cdomment_time">'.$date.'</span><br/>
        <div class="avatarcnt">
        <img alt="" src="uploadprofile/'.$u_imgurl.'"/>
        </div></div></div>
        <div class="cdomment_text">';
        if ($description=="") {echo '';}
        else echo''.htmlentities($description).'<br>';
        echo '</div>';


//reply
echo'<div class="reply_box"><a href="javascript: void(0)" class="reply" id="'.$id.'">Reply</a></div>';

//Likes
echo'<div></div>'; 

// Only Reply 
$query = "SELECT * FROM comments_reply WHERE parent_id ='".$id."' ORDER BY id DESC";
$res = mysqli_query($dbh,$query);
while($row = mysqli_fetch_array($res)){   
$id = $row['id'];
$qazi_id = $row['qazi_id'];
$likes = $row['likes'];
$username = $row['username'];
$img = $row['img'];
$description = $row['description'];     
$url = $row['url'];
$parent_id = $row['parent_id'];
$date = $row['date'];

        echo' <div class="rcontent"><replycomment><ul>
        <div class="comment">
        <div class="cdomment_cheder">';
        if ($url=="") {
        echo'<p class="name">'.$username.' Says:</p>'; }
        else  echo'<p class="name"><a href="userinfo.php?user='.$url.'" title="'.$username.'">'.$username.'</a> Says:</p>';
        echo'<span class="cdomment_time">'.$date.'</span><br/>
        <div class="avatarcnt">
        <img alt="" src="uploadprofile/'.$u_imgurl.'"/>
        </div></div></div>
        <div class="cdomment_text">';
        if ($description=="") {echo '';}
        else echo''.htmlentities($description).'<br>';
        echo '</div>';

        </ul><replycomment></div>'; 

}   //reply close   
}    //comment close
?>

// REPLY FORM
<div id="loader"></div><div class="reply_here" id="reply_here-<?php echo $rows['id'] ?>" style="display:block;">
    <form action="" method="post" id="repfrm">
      <fieldset id="cmntfs">
        <legend class="pyct">Your comment</legend>
        <input type="hidden" name="username" id="author" value="<?php echo $_SESSION['username'] ?>"/>
        <input type="hidden" name="url" id="url" value="" />
        <div id="maintbox">
        <textarea name="replycom" rows="2" id="replycom"></textarea>
        </div>
        <input type="hidden" name="parent_id" id="parent_id" value="0" />
        <input type="hidden" name="tutid" id="tutid" value="<?php echo $tutid ?>" />
        <button type="submit" name="submit" value="" id="submit" class="repfrm">Post Reply</button>
      </fieldset>
    </form>
</div>
<?
echo'</div><comment></div>';

//pagination 

2 个答案:

答案 0 :(得分:0)

如果我理解正确您的问题是您有多个回复文本区域,但只有其中一个有效。我想从最简单的错误开始,我说这是因为你在回复表单中只有一个id为textarea。

HTML将拉出它看起来的第一个id /名称,如果它们共享相同的名称,则丢弃其余的名称,如果第一个为null,那么就是发布的内容。

所以在某些时候你需要迭代'replycom'名称/ id 这只是一个例子

 $replyNumber = 0;

...


<textarea name="replycom$replyNumber" rows="2" id="replycom$replyNumber"></textarea>

修改 我记得你可以在HTMl中将变量定义为一个数组

<textarea name="replycom[]" rows="2" id="replycom"></textarea>

答案 1 :(得分:0)

通过添加动态添加文本值的JavaScript来解决问题。感谢Sir Useless Intern也在这里评论我。

$(".replycom").bind("keyup", function() {
$(".replycom").text($(this).val());
});