python小菜 2018-11-15 04:19 采纳率: 0%
浏览 15

Ajax评论未重新提交

Good day all. I have this code that works pretty well, but the problem is, when I change the values of the input field, It does not submit the new data to the server, rather the old data re resubmitted to the server, which is echo out. Nww input is only submitted after the page is refreshed. Wihich defeat the initial purpose of using ajax. Please somebody should point me in the right direction. Thanks in advance. Here is my code.

<link rel="stylesheet" href="comment.css">
<div class="l-com" id="l-com">
    <a class="clickComment" href="#">Leave a comment</a>
</div>
<div class="comment-container">
    <p>
       <input type="text" placeholder="Enter your name" id="name">
    </p>
    <p>
        <input type="text" placeholder="Enter your email" id="email">
    </p>
    <p>
       <textarea id="comment" placeholder="Write your comment" rows="7"> 
       </textarea>
    </p>
    <p>
       <button type="submit" id="submitBtn" 
       onclick="showComment();">Post</button>           
    </p>

    </div>
  <div id="comment-result">  
  </div>

JavaScript

    var name = document.getElementById('name').value,
    email = document.getElementById('email').value,
    comment = document.getElementById('comment').value,
    pageUrl = window.location.href,
    lastFsl = pageUrl.lastIndexOf('/'),
    partUrl = pageUrl.substring(lastFsl + 1),
    addrDot = partUrl.indexOf('.'),
    pageLocation = partUrl.substring(0, addrDot),
    webpage = pageLocation.replace(/-/g, '_'),
    leaveAcomm = document.querySelector('.clickComment'),
    commentWrapper = document.querySelector('.comment-container'),
    submitBtn = document.getElementById('submitBtn'),
    request = new XMLHttpRequest(),

    parameters = 'name=' + encodeURIComponent(name) + 
          '&email=' + encodeURIComponent(email) +
          '&comment=' + encodeURIComponent(comment) + 
          '&webpage=' + encodeURIComponent(webpage);

          leaveAcomm.addEventListener('click', ChangeDisplay);
          function ChangeDisplay() {
              elDisplay = commentWrapper.style.display;
              if(elDisplay === 'block') {
                  commentWrapper.style.display = 'none';
              }else {
                  commentWrapper.style.display = 'block';
              }
          }


    function showComment() {
        request.open('POST','comment.handler.php', true);
        request.setRequestHeader('Content-Type', 'application/x-www-form- 
        urlencoded');

        request.onreadystatechange = function () {
        if(request.readyState == 4 && request.status == 200) {
            var commentResult = document.getElementById('comment-result');
            commentResult.innerHTML = request.responseText;
            console.log(request.responseText);
       }

    }

    request.send(parameters);   
}
//php code comment.handler.php

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$comment = $_POST['comment'];
$table = $_POST['webpage'];
$date = time();


//connect to the database 

$servername = 'localhost';
$username = 'root';
$password = '';
$db = 'commdb';

    $conn = mysqli_connect($servername, $username,$password, $db);
   if(!$conn) {
       die('could not connect ' . mysqli_connect_error($conn));
   }
$checkTable = "SELECT 1 FROM $table";
$chkTblRun = mysqli_query($conn, $checkTable);
if($chkTblRun !== FALSE) {
    if(!empty($name) && !empty($comment) && !empty($email)){
        $insertComment = "INSERT INTO $table(posterName, email, comment, 
         commentDate) 
         VALUES ('$name', '$email', '$comment', '$date')";
         $insertComRun = mysqli_query($conn, $insertComment);
         $selAll = "SELECT * FROM $table ORDER BY ID DESC";
         $selAllRun = mysqli_query($conn, $selAll);
         while($rows = mysqli_fetch_assoc($selAllRun)) {
            $name_field = $rows['posterName'];
            $comment_field = $rows['comment'];
            $postDate = $rows['commentDate'];

            echo '<section id="commResults">' . '<div id="postName">' . 
            $name_field . 
            '</div>'. '<div id="commenter">' . $comment_field . '</div>' .
            '</section>';
         }
    }
  } else {
        $createTable = "CREATE TABLE $table (" .
        "ID INT NOT NULL AUTO_INCREMENT," .
        "posterName VARCHAR (50) NOT NULL," .
        "email VARCHAR (50) NOT NULL," .
        "comment TEXT NOT NULL," .
        "commentDate TEXT NOT NULL," .
        "PRIMARY KEY (ID)" . 

        ")";

      $crtTblRn = mysqli_query($conn, $createTable);
      if(!$createTable) {
      echo 'could not create table ' . mysqli_error($conn);
     } else {
            if(!empty($name) && !empty($comment) && !empty($email)) {
            $insertComm = "INSERT INTO $table (posterName, email, comment, 
            commentDate)
            VALUES('$name', '$email', '$comment', '$date')";
            $insertComRun = mysqli_query($conn, $insertComm);
            $selAll = "SELECT * FROM $table ORDER BY ID DESC";
            $selAllRun = mysqli_query($conn, $selAll);
            while ($rows = mysqli_fetch_assoc($selAllRun)) {
                  $name_field = $rows['posterName'];
                  $comment_field = $rows['comment'];
                  $postDate = $rows['commentDate'];

                  echo '<section id="commResults">' . '<div id="postName">' . 
                  $name_field . 
                  '</div>' . '<div id="commenter">' . $comment_field . 
                  '</div>' .
                  '</section>';
             }
        }
      }
    }

?>

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
    • ¥15 微信会员卡接入微信支付商户号收款
    • ¥15 如何获取烟草零售终端数据
    • ¥15 数学建模招标中位数问题
    • ¥15 phython路径名过长报错 不知道什么问题
    • ¥15 深度学习中模型转换该怎么实现
    • ¥15 HLs设计手写数字识别程序编译通不过
    • ¥15 Stata外部命令安装问题求帮助!
    • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
    • ¥15 TYPCE母转母,插入认方向