我正在尝试创建一个帖子和评论部分,用户可以在其中发表和评论。为此,我有2个表格:posts,comment
在我的帖子表中有两列:id和posts;它是这样的:
id | post
----------------------
1 | lo;l;l
-----------------------
2 | i am feeling well
在我的评论表中,有三列:id,comment和post_id(这是用户在其中发表评论的帖子的ID,这样我就可以每次动态检索到每个帖子的评论)
我管理了帖子部分,但无法在评论时弄清楚如何获取特定帖子的ID。我不需要任何直接的解决方案,而是PDO中有关如何执行此操作的工作大纲。在这方面的任何帮助将不胜感激
这是我到目前为止所管理的(可能会有点长,但是真的很容易理解。我没有提供我的数据库查询页面来保持帖子的较小。我需要知道如何在我正在的时候获取特定的post_id评论)
我还没有写ajax调用内的insertComment.php页面。但是我希望我的注释可以插入并检索功能。
<?php
session_start();
require_once 'myDB.php';
if(isset($_POST['post']) && !empty($_POST['post'])){
$post=$_POST['post'];
$_SESSION['post']=$_POST['post'];
try{
$newComment=DB::getInstance();
$newComment->insert('post',array('posts'=>$post));
}catch(Exception $e){
echo $e->getMessage();
}
header('HTTP/1.1 303 see other');
header('Location:'.$_SERVER['PHP_SELF']);
}
?>
<html>
<head>
<style>
#formdiv{width:347px;height:120px;background:#dfe3ee;position:relative;border:1px dashed black;top:0px;left:300px;padding:5px; margin-bottom:3px;
}
#cmntbox{
width:347px;background:#dfe3ee;position:relative;border:1px solid black;left:300px;padding:5px;
}
.repText{
width:100%;background:#f7f7f7;position:relative;border:1px solid black;padding:3px;resize:none;}
}
</style>
</head>
</body>
<div id='formdiv'>
<form action='' method='POST'>
<textarea name='post' placeholder="what's on your mind !" cols='40' rows='3'></textarea>
<input type='submit' name='submit' value='post comment' style='float:right;width:100px;height:30px;background:#3b5998;color:white;'>
</form>
</div>
<?php
$newComment=DB::getInstance();
$results=$newComment->getComment('SELECT','post','posts')->result();
foreach($results as $result=>$val){
?>
<div id='cmntbox'><?php
echo $val->posts;
echo '</br><hr>';?>
<form>
<textarea name='myrep' id='myreply' class='repText'></textarea>
<input type='button' class='reply' value='reply' style='width:50px;height:30px;background:#3b5998;color:white;' >
</form>
</div>
<?php
}
?>
<script>
var reply=document.getElementsByClassName('reply');
var repText=document.getElementsByClassName('repText');
for(i=0;i<reply.length;i++){
(function(i){
reply[i].addEventListener('click',function(e){
var xmlHttp=new XMLHttpRequest();
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4 && xmlHttp.status==200){
//do nothing
}else{
alert('there was a problem ');
}
}
var parameters='myrep='+document.getElementById('myreq').value
xmlHttp.open("POST", "insertcomment.php", true);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.send(parameters);
}
});
})(i);
}
</script>
</body>
</html>
您将在页面加载时获得帖子ID,因此在html中绘制注释框时,您可以将帖子ID设置为某些html元素的属性或隐藏字段中的属性
因此,在您的情况下,请更新以下脚本
<div id='cmntbox'><?php
echo $val->posts;
echo '</br><hr>';?>
<form>
<textarea name='myrep' id='myreply' class='repText'></textarea>
<input type='button' class='reply' value='reply' style='width:50px;height:30px;background:#3b5998;color:white;' >
</form>
</div>
到
<div id='cmntbox'><?php
echo $val->posts;
echo '</br><hr>';?>
<form id="<?php echo $val->postId ?>">
<textarea name='myrep' id='myreply' class='repText'></textarea>
<input type='button' class='reply' value='reply' style='width:50px;height:30px;background:#3b5998;color:white;' >
</form>
</div>
或者
<div id='cmntbox'><?php
echo $val->posts;
echo '</br><hr>';?>
<form>
<input type="hidden" name="postId" value="<?php echo $val->postId ?>" />
<textarea name='myrep' id='myreply' class='repText'></textarea>
<input type='button' class='reply' value='reply' style='width:50px;height:30px;background:#3b5998;color:white;' >
</form>
</div>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句