Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/473.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript can';我不知道如何在提交时显示最新评论,而不重新查看页面_Javascript_Php_Html_Ajax_Comments - Fatal编程技术网

Javascript can';我不知道如何在提交时显示最新评论,而不重新查看页面

Javascript can';我不知道如何在提交时显示最新评论,而不重新查看页面,javascript,php,html,ajax,comments,Javascript,Php,Html,Ajax,Comments,我正在尝试制作一个帖子和评论脚本。只是为了学习。到目前为止,我所做的是: 通过单击提交按钮发布状态 (它会将帖子插入数据库,并从数据库中检索帖子以显示) 每个帖子都会有一个评论区,它会把用户的评论上传到数据库(问题就在这里)。 我的主要问题是,当用户单击“发布”按钮时,我无法显示最近发布的评论。当用户单击“发布”按钮时,它将被插入数据库,但在更新时无法向他们显示。我如何才能做到这一点。我需要建议或想法……不是编码的解决方案。谢谢:) 问题是在红色圆圈区域,用户将发表评论并单击post按钮。它将

我正在尝试制作一个帖子和评论脚本。只是为了学习。到目前为止,我所做的是:

通过单击提交按钮发布状态 (它会将帖子插入数据库,并从数据库中检索帖子以显示)

每个帖子都会有一个评论区,它会把用户的评论上传到数据库(问题就在这里)。 我的主要问题是,当用户单击“发布”按钮时,我无法显示最近发布的评论。当用户单击“发布”按钮时,它将被插入数据库,但在更新时无法向他们显示。我如何才能做到这一点。我需要建议或想法……不是编码的解决方案。谢谢:)

问题是在红色圆圈区域,用户将发表评论并单击post按钮。它将通过使用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>

只需在div上添加注释。。。它会自动更新为什么你要经历所有这些麻烦,使用JS框架(如Angular或ReAct)构建它插入后不必选择注释。只是在
DOM
@NullPoiцteц中插入的注释?你指的是ajax函数中的javascript?@HarshalCarpenter不熟悉这些框架。此外,它帮助我更好地理解语言。感谢你的建议。将期待学习这些框架:)只需在div上添加注释。。。它会自动更新为什么你要经历所有这些麻烦,使用JS框架(如Angular或ReAct)构建它插入后不必选择注释。只是在
DOM
@NullPoièteè中插入的注释?你是说ajax函数中的javascript?@HarshalCarpenter不熟悉这些框架。此外,这有助于我更好地理解该语言。感谢你的建议。期待着学习这些框架:)
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);

   }
}