几天来,我一直在拼命地向评论区添加回复字段--基本上,我希望只要你点击某个按钮,回复字段(Div)就会出现。我认为使用onclick事件处理程序是可行的。
我们已经在一个php回显中了,所以我认为只需将row-id (cid)添加到实际的div和button id中会很容易-事实证明它不起作用。
将myFunction()添加到按钮
添加了"myDIV“来检查代码是否可以与这个简单的div一起工作-它确实可以
添加了ID以打印出div和按钮的名称,同时添加了" cid“以在浏览器中获得更好的概述( brwoser中的cid正如预期的那样)-最后,我摆脱了代码,只是提醒了cid,并且非常惊讶地发现它是一个我没有预料到的数字。它是数据库中的最后一个cid。
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>
<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';
}
}
";
我“简单地”想要一个文本区出现在我点击“回复”的评论下面-据我所知,我必须在php while循环中做这件事,因为我从数据库收到评论。
任何帮助都将不胜感激
Thx的家伙
发布于 2019-04-25 03:33:47
这是您使用jquery所需的解决方案。现在你可以在这里使用php来处理动态数据了。
$(document).ready(function(){
$("button").click(function(){
$("#replyField").fadeToggle();
});
});
button{
display:block;
}
#replyField {
display:none
}
<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>
https://stackoverflow.com/questions/55836906
复制相似问题