简体   繁体   English

使用Ajax插入和获取数据插入有效,但获取功能无效

[英]using Ajax to insert and fetch the data insert is working but fetch function is not working

I'm using ajax insert and fetch the data from the database, insert is working perfectly but fetching part is not working give a feedback to fix this issues. 我正在使用ajax插入并从数据库中获取数据,插入工作正常,但获取部分无法正常工作,请提供反馈来解决此问题。

<script>
            $(document).ready(function(){
                $("#button").click(function(e){
                   e.preventDefault();
                    var postId=$("#postId").val();
                    var userId=$("#userId").val();
                    var postComm=$("#postComments").val();

                    $.ajax({
                        url:'../validate/inserPostComm.php',
                        method:'POST',
                        data:{
                            poId:postId,
                            usId:userId,
                            poco:postComm
                        },
                       success:function(data){
                           //alert(data);
                           displayFromDatabase();
                           $("#postComments").val('');
                       }
                    });
                });
            });

              function displayFromDatabase(){
                    var postId=$("#postId").val();
                        alert(postId);
                  $.ajax({
                      url: "../validate/getComments.php",
                      type: "POST",
                      async: false,
                      data: {
                            poId:postId,
                      },
                      success: function(data){
                        ('#display_area').html(data);  
                   }
                  });
              }
          </script>

and this my html code to retrieve the fetching details from database. 这是我的html代码,用于从数据库中检索详细信息。

<li>
  <div id="display_area">

   </div>
</li> 
<button type="button" id="button"><i class="fa fa-paper-plane"></i></button>

and also i attached my php code through the ajax i'm passing the id and i'm get the details according to the id. 而且我通过ajax附加了我的php代码,我传递了ID,并且根据ID获得了详细信息。

$postId=$_POST["poId"];

$getPostCom=$postComments->getPostComm($postId,"../");

while($PostComments=mysqli_fetch_assoc($getPostCom))
 {

    ?>


    <div class="comet-avatar">
        <img src="<?php echo $PostComments["u_image"]; ?>" alt="">
    </div>
        <div class="we-comment">
            <div class="coment-head">
                <h5><a href="user-profile.php?user_id=<?php echo $PostComments["u_id"]; ?>" title=""><?php echo $PostComments["u_fname"]; ?> <?php echo $PostComments["u_lname"]; ?></a></h5>
            </div>
                <p><?php echo $PostComments["p_comments"]; ?></p>
        </div>

<?php
 }
    exit();
?>

Yes this code working perfectly but i missed the $ in the success block. 是的,此代码可以完美运行,但是我错过了成功代码中的$。

<script>
            $(document).ready(function(){
                $("#button").click(function(e){
                   e.preventDefault();
                    var postId=$("#postId").val();
                    var userId=$("#userId").val();
                    var postComm=$("#postComments").val();

                    $.ajax({
                        url:'../validate/inserPostComm.php',
                        method:'POST',
                        data:{
                            poId:postId,
                            usId:userId,
                            poco:postComm
                        },
                       success:function(data){
                           //alert(data);
                           displayFromDatabase();
                           $("#postComments").val('');
                       }
                    });
                });
            });

              function displayFromDatabase(){
                    var postId=$("#postId").val();
                        alert(postId);
                  $.ajax({
                      url: "../validate/getComments.php",
                      type: "POST",
                      async: false,
                      data: {
                            poId:postId,
                      },
                      success: function(data){
                        $('#display_area').html(data);  
                   }
                  });
              }
          </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM