douzao2992 2019-07-05 04:24
浏览 364
已采纳

为什么我的新内容不会立即在页面上显示/消失,而是在手动刷新后显示?

I am sending requests to a database via AJAX with vanilla JS. The data is written in the database without issues. I cannot see the content being put in the database until I refresh the page. The same happens when I delete content, I have to refresh in order to see the content gone.

I've read and watched a few tutorials that say what to do, and I don't seem to find the error in my code. I can click a button and load "temporary" code on the fly, but when I get the database involved, I cannot recreate the same functionality.

JavaScript

function postMessage(name, message){
      var author = document.getElementById(name).value;
      var message = document.getElementById(message).value;

      var xhr = ajaxObj("POST", location.href);
      xhr.onreadystatechange = function(){
        if(ajaxReturn(xhr) == 'posted'){
          var currentHTML = document.getElementById('comments').innerHTML;
          document.getElementById('comments').innerHTML = '<div id="post_'+id+'"><p>'+author+' : '+'</p><p>'+message+'</p><button id="message_'+id+'" onclick="deleteMessage('+id+')">Delete Message</button>' + data;
          document.getElementById(message).value = '';
        }
      }
      xhr.send("action=postMessage&author="+author+"&message="+message);
    }

html/php

<form class="" action="index.html" method="post">
      <input type="text" name="name" id="name" value="" placeholder="Name"><br/>
      <textarea name="message" id="message" rows="8" cols="80"></textarea><br/>
      <button type="button" name="button" onclick="postMessage('name', 'message')">Post Message</button>
    </form>
    <div id="comments">
      <?php
      if(isset($_POST['action']) && $_POST['action'] == 'postMessage'){
        $author = $_POST['author'];
        $message = $_POST['message'];
        $sql = "INSERT INTO comments (author, message) VALUES ('$author', '$message')";
        $db_link->query($sql);
        $db_link->close();
        return "posted";
        exit();
      }
      $all_posts = $db_link->query("SELECT * FROM comments");
      while($post = $all_posts->fetch_assoc()){
        $author = $post['author'];
        $message = $post['message'];
        $date = $post['entered'];
        $post_id = $post['id'];
        $new_post .= "<div id='post_'$post_id>
        <p>$author : $date</p>
        <p>$message</p>
        <button id='message_$post_id' onclick='deleteMessage($post_id)'>Delete Message</button>";
      }
      echo $new_post;
      ?>
    </div>

The query is successfully executed on the database but I don't see the new div with the message until I manually refresh the page. There are no errors logged in the PHP code or in the console for JS.

  • 写回答

1条回答 默认 最新

  • doujia7094 2019-07-05 04:39
    关注

    Create a new file to submit data like save.php

    and use xhr.onload to check whether execution is successful or not

    function postMessage(name, message){
        var author = document.getElementById(name).value;
        var message = document.getElementById(message).value;
    
        var xhr = ajaxObj("POST", 'save.php');
    
        xhr.onload = function () {
            var currentHTML = document.getElementById('comments').innerHTML;
            document.getElementById('comments').innerHTML = '<div id="post_'+id+'"><p>'+author+' : '+'</p><p>'+message+'</p><button id="message_'+id+'" onclick="deleteMessage('+id+')">Delete Message</button>' + data;
            document.getElementById(message).value = '';
    
        };
    
        xhr.send("save.php/action=postMessage&author="+author+"&message="+message);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?