truthlies uchiha
truthlies uchiha

Reputation: 169

Get data after submit form in database using jquery

I have a page of post message of all users.

enter image description here

It is possible to submit form and get it automatic in database? because i did not use input type for date and i leave current timestamp as a default in database.

enter image description here

form with 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 
    } 
 ?>

After I submit post it will also appear with other post message of other users by getting the id = postcomment in the div element

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("");                    
              });
          });         
   });
 });

I insert the post message in postcomment.php and then load it in showpostcomment.php with the same code as below form with user_id

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";   

     ?>

Upvotes: 1

Views: 535

Answers (1)

SRK
SRK

Reputation: 3496

Try this query in your showcomment.php

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

This way you will only get the last inserted record.

Change below code.

    <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>

Also this one.

$(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("");                    
              });
          });         
   });
 });

Upvotes: 1

Related Questions