我正在尝试发帖和评论脚本。只是为了学习目的。到目前为止我做的是:
(它会将帖子插入数据库并从数据库中检索以显示)
我的主要问题是我无法显示用户点击发布按钮时发布的最新评论。当用户点击发布按钮时,它将被插入到数据库中,但在更新时无法显示给他们。我怎么能这样做。我需要建议或想法......不是编码解决方案。谢谢:)
问题是在红色圆圈区域用户将发表评论并点击发布按钮。将使用firendComment.php页面通过ajax调用将其上传到数据库。但是不能通过显示上传的评论来更新帖子吗?我怎么能这样做。为方便起见,我在这里提供我的整个脚本:
<?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;
}
#individual{background:#f7f9f7;display:block;height:30px;padding:6px;}
#cmntbox{
width:347px;background:#dfe3ee;position:relative;border:1px solid black;left:300px;padding:5px;margin-bottom:10px;
}
.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',array('posts','id'))->result();
foreach($results as $result=>$val){
$postid='rep'.$val->id;
$hiddenid='hidden'.$val->id;
?>
<div id='cmntbox'><?php
echo $val->posts;
echo '</br><hr>';
$comments=$newComment->get('cmnt',array('postid','=',$val->id))->result();
foreach($comments as $comment){
?>
<div id='individual'><?php echo $comment->cmnts; ?></div>
<hr>
<?php
}
?>
<form action='friendComment.php' method='post'>
<textarea name='myrep' id='<?php echo $postid; ?>' class='repText'></textarea>
<input type='button' class='reply' value='reply' style='width:50px;height:30px;background:#3b5998;color:white;' >
<input type='hidden' id='<?php echo $hiddenid; ?>' class='hiddenfield' name='postid' value='<?php echo $val->id; ?>'>
</form>
</div>
<?php
}
?>
<script>
var reply=document.getElementsByClassName('reply');
var repText=document.getElementsByClassName('repText');
var hidden=document.getElementsByClassName('hiddenfield');
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){
console.log(xmlHttp.responseText);//do nothing
}
}
var parameters='myrep='+repText[i].value+'&postid='+hidden[i].value;
xmlHttp.open("POST", "friendComment.php", true);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.send(parameters);
});
})(i);
}
</script>
</body>
</html>
friendComment.php页面:
session_start();
require_once 'myDB.php';
if(isset($_POST['myrep']) && isset($_POST['postid'])){
if(!empty($_POST['myrep']) && !empty($_POST['postid'])){
// echo $_POST['postid'].' has a comment of '.$_POST['myrep'];
$getDB=DB::getInstance();
$getDB->insert('cmnt',array('postid'=>$_POST['postid'],'cmnts'=>$_POST['myrep']));
$results=$getDB->getComment('SELECT','cmnt',array('postid','cmnts'));
print_r($results);
}
}