我正在开发即时评论系统。我在请求中不断遇到status text
错误。提交的数据进入服务器,但生成JavaScript错误ajax error:1
。
在下面的前两个代码块中,我从表单插入数据并发回Ajax请求的数据。第三部分是我的PHP标记。
我还想确定Ajax调用是否将在正确的位置(即entered_comments
分区内)插入接收到的数据,还是应该在community.php
中也包含echo语句?
JS:
request.onreadystatechange=function (){
if(this.readyState==4){
if(this.status==200){
if(this.responseText!=null){
document.getElementById("entered_comments").innerHTML=this.responseText
document.getElementById('comment_text').value=''
}
else alert("ajax error:no data received")
}
else alert("ajax error:1"+ this.statusText)
}
}
var submit_form=document.getElementById('form');
submit_form.onsubmit=function ajaxfire(){
request.send(params)
}
//ajax request
function ajaxRequest(){
try{
var request=new XMLHttpRequest()
}
catch(e1){
try{
request= new ActiveXObject("Msxml2.XMLHTTP")
}
catch(e2)
{
try{
request=new ActiveXObject("Microsoft.XMLHTTP")
}
catch(e3){
request=false
}
}
}
return request
}
PHP:
//ajax_comment.php-inserting and retrieving from server
<?php
require_once "login.php";
if (isset($_POST["comment_text"])&& isset($_POST["name"]))
{
$comment=$_POST['comment_text'];
$name=$_POST["name"];
$query="INSERT INTO comment (text,id) VALUES ('$comment', NULL)";
$result=$conn->query($query);
if (!$result) die("database insert set1 failed".$conn->error);
$query="INSERT INTO users (names,id) VALUES ('$name', NULL)";
$result=$conn->query($query);
if (!$result) die("database insert set2 failed".$conn->error);
}
else{
die($conn->error);
}
$query='SELECT text,id FROM comment';
$result=$conn->query($query);
if(!$result) die('database retrieval error:'.$conn->error);
if ($result->num_rows>0)
{
while($row=$result->fetch_array()){
echo $row['text'];
}
}else{
echo "0 results";
}
$conn->close();
?>
HTML:
//community.php-this is html page displayed
<!DOCTYPE HTML>
<!--[if IE 7 ]> <html class="ie7"> <![endif]-->
<!--[if IE 8]> <html class="ie8"> <![endif]-->
<html lang="en">
<head>
<meta charset="utf-8">
<title>Community</title>
<link rel="stylesheet" href="sheets/mainalma.css" type="text/css">
<link rel="stylesheet" href="assets/css/all.css">
</head>
<body>
<!--header-->
<header>
<div class="grid-1"><a href="landing.html">ALMANAC<br>BLOG OF
LIFE</a></div>
<div class="dropdown">
<button class="dropbtn grid-2" onclick="myFunction()">
MENU <i class="fas fa-bars"></i>
</button>
<div class="dropdown-content" id="myDropdown">
<a href="landing.html">HOME</a>
<a href="login.html">WRITERS LOGIN</a>
<a href="search.html">SEARCH STORY</a>
</div>
</div>
</div>
</header>
<img class="images-community" src="assets/images/community.jpg"
alt="community">
<h1 class="header">Community</h1>
<h2 class="header">By Adetunji Bunmi</h2>
<p>Today in our community, many people do not care about
</p>
<!--comment and replies-->
<form action="" method="post" id="form">
<fieldset>
<label>
COMMENT<br>
<textarea name="comment_text" class="textarea"
id="comment_text"></textarea>
</label>
</fieldset>
<fieldset>
<label>
NAME<br>
<input type="text" name="name" maxlength="40" class="name"><br>
</label>
</fieldset>
<fieldset>
<input type="submit" name="submit_button" value="SUBMIT"
class="submit" id="submit_button">
</fieldset>
</form>
<!--footer section-->
<!--entered comments-->
<div class="comment-section">
<p id="entered_comments"></p>
</div>
<footer>
<nav>
<ul>
<li>Home</li><li>
Writers Login</li><li>
Search Story</li>
</ul>
</nav>
<small>© Almanac Blog | Tunjiz Concept</small>
</footer>
<script type="text/javascript" src="scripts/dropdown_menu.js">
</script>
</body>
</html>