使用jquery在数据库中提交表单后获取数据

时间:2018-03-15 07:01:04

标签: php jquery

我有一页所有用户的帖子。

enter image description here

可以提交表单并在数据库中自动获取吗?因为我没有使用输入类型作为日期,我将当前时间戳保留为数据库中的默认值。

enter image description here

带有show post message的表单

<form  id="formsubmitcomment">
  <input type="hidden" id="user_id" name="user_id" value="<?php echo $user_id?>">
  <textarea name="post" id="post" style="resize: none" placeholder="What's  on your mind">
  </textarea>
  <input type="button" id="submit" name="submit" value="Post">
</form>

<?php
  require './database.php';
     $showpostcomment = mysqli_stmt_init($connection);
     $sql = "SELECT user_id, post_comment from postcomment ORDER BY postcomment_id DESC";
     mysqli_stmt_prepare($showpostcomment, $sql);

      mysqli_stmt_execute($showpostcomment);
      mysqli_stmt_bind_result($showpostcomment, $user_id, $post_comment);
      mysqli_stmt_store_result($showpostcomment);

      while (mysqli_stmt_fetch($showpostcomment)) 
     {
?>
        <div class="w3-panel w3-white" id="postcomment">
           <p id="comment"><?php echo $post_comment ?></p>  
        </div>
<?php 
    } 
 ?>

在我提交帖子之后,通过在div元素中获取id = postcomment,它也会显示其他用户的其他帖子消息

jquery的

   $(document).ready(function() {
    $("#submit").click(function (){
    var post = $("#post").val();

    $.post("postcomment.php", $("#formsubmitcomment").serialize(), function(){
      $("#postcomment").load("showpostcomment.php", function(data){
      $("#comment").prepend(data + "br");
      $("#post").val("");                    
              });
          });         
   });
 });

我在postcomment.php中插入帖子消息,然后使用与user_id相同的代码将其加载到showpostcomment.php中

showpostcomment.php

 require './database.php';
       $user_id = $_SESSION["user_id"];
      $showpostcomment = mysqli_stmt_init($connection);
  $sql = "SELECT user_id, post_comment from postcomment WHERE user_id = ? ORDER BY postcomment_id DESC";
      mysqli_stmt_prepare($showpostcomment, $sql);
      mysqli_stmt_bind_param($showpostcomment, "i", $user_id);
      mysqli_stmt_execute($showpostcomment);
      mysqli_stmt_bind_result($showpostcomment, $user_id, $post_comment);
      mysqli_stmt_store_result($showpostcomment);

      echo $post_comment;
     mysqli_stmt_fetch($showpostcomment);

        echo "$post_comment";   

     ?>

1 个答案:

答案 0 :(得分:1)

在showcomment.php中尝试此查询

SELECT user_id, post_comment from postcomment WHERE user_id = ? ORDER BY postcomment_id DESC LIMIT 1;

这样您只能获得最后插入的记录。

更改以下代码。

    <form  id="formsubmitcomment">
      <input type="hidden" id="user_id" name="user_id" value="<?php echo $user_id?>">
      <textarea name="post" id="post" style="resize: none" placeholder="What's  on your mind">
      </textarea>
      <input type="button" id="submit" name="submit" value="Post">
    </form>

        <?php
          require './database.php';
             $showpostcomment = mysqli_stmt_init($connection);
             $sql = "SELECT user_id, post_comment from postcomment ORDER BY postcomment_id DESC";
             mysqli_stmt_prepare($showpostcomment, $sql);

              mysqli_stmt_execute($showpostcomment);
              mysqli_stmt_bind_result($showpostcomment, $user_id, $post_comment);
              mysqli_stmt_store_result($showpostcomment);
        ?>
             <div class="w3-panel w3-white" id="postcomment">
          <?php
              while (mysqli_stmt_fetch($showpostcomment)) 
             {
        ?>

                   <p class="comment"><?php echo $post_comment ?></p>  

        <?php 
            } 
         ?>
         </div>

也是这一个。

$(document).ready(function() {
    $("#submit").click(function (){
    var post = $("#post").val();

    $.post("postcomment.php", $("#formsubmitcomment").serialize(), function(){
      $("#postcomment").load("showpostcomment.php", function(data){
      $("#postcomment").prepend("<p class='comment'>"+ data + "</p>");
      $("#post").val("");                    
              });
          });         
   });
 });