我正在做一个学校作业,我们基本上必须重新创建Instagram。现在,我无法在数据库中添加注释并使用AJAX更新前端。
我的工作基于我们在学校做的一项运动,效果很好。但是我现在看不到出什么问题了。
当我取出整个jQuery脚本时,PHP会将数据推送到数据库中而没有任何问题。我已经进行了大约2小时的故障排除,并且找不到任何在线内容,尝试了验证器,但是一切似乎都很正常。
有什么想法吗? 预先感谢
!!!编辑:我刚发现问题!但是不知道如何解决这个问题。正如我在第3个文件(comment.php)中评论的那样,include由于某种原因阻止了代码。当我忽略它时,AJAX可以正常工作,但是什么也没推送到数据库中。有什么想法吗?
post.php(带有图像和注释部分的页面)
<?php
require_once("bootstrap.php");
Session::check();
require_once("includes/checklogin.inc.php");
$postId = $_GET['id'];
if(!empty($_POST)){
try{
$comment = new Comment();
$newComment = $_POST['NewComment'];
$comment->setPostId($postId);
$comment->setText($newComment);
$comment->save();
}catch(Exception $e) {
//Catch Statement
}
}
?>
<!doctype html>
<html lang="en">
<?php require_once("includes/header.inc.php"); ?>
<body>
<?php require_once("includes/nav.inc.php"); ?>
<div class="container">
<aside>
<article id="commentList"> <!-- COMMENT SECTION -->
<?php foreach($post->getComments() as $c): ?>
<p>
<span <?php if($c['comment_user_id'] == $_SESSION['user']){echo "class=\"yellow\"";}?>>
<?php echo $c['firstname'] . " " . $c['lastname']; ?>
</span>
<?php echo ": " . $c['comment_text']; ?>
</p>
<?php endforeach; ?>
</article>
<form method="post" action=""> <!-- ADD COMMENT -->
<input type="text" id="NewComment" name="NewComment" placeholder="add a comment" required>
<input type="submit" id="submit" value="send">
</form>
</aside>
</div>
</body>
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script>
$(document).ready(function(){
$("#submit").on("click", function(e){
var text = $("#NewComment").val();
$.ajax({
method: "POST",
url: "ajax/comment.php",
data: {text: text},
dataType: "json"
})
.done(function( res ) {
if(res.status == "success") {
<?php foreach($post->getUsername() as $u): ?>
var p =
"<p><span class=\"yellow\"><?php echo $u['firstname'] . ' ' . $u['lastname']; ?></span>: " + text + "</p>";
<?php endforeach; ?>
$("#commentList").append(p);
$("#NewComment").val("").focus();
}
});
e.preventDefault();
});
});
</script>
</html>
comment.class.php(在数据库中推送注释的后端,效果很好)
<?php
class Comment{
private $postId;
private $text;
// POST ID
public function getPostId(){
return $this->postId;
}
public function setPostId($postId){
$this->postId = $postId;
return $this;
}
// COMMENT
public function getText(){
return $this->text;
}
public function setText($text){
$this->text = $text;
return $this;
}
// ADD COMMENT TO DB
public function save(){
$conn = Db::getInstance();
$conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$statement = $conn->prepare("
INSERT INTO comments (comment_user_id, comment_text, comment_post_id, comment_date)
VALUES (:user_id, :text, :post_id, :date)
");
$statement->bindValue(":post_id", $this->getPostId());
$statement->bindValue(":user_id", $_SESSION['user']);
$statement->bindValue(":text", $this->getText());
$statement->bindValue(":date", strftime("%Y-%m-%d %H:%M:%S"));
$statement->execute();
return true;
}
}
?>
comment.php(AJAX所指的文件)
<?php
include("../bootstrap.php");
// THIS IS A SPECIAL ONE, WHEN INCLUDED NOTHING HAPPENS ON THE FRONTEND, WHEN REMOVED MY COMMENT GETS ADDED BUT NOT PUSHED INSIDE DATABSE
if(!empty($_POST)){
$text = $_POST['text'];
try{
$comment = new Comment();
$comment->setText($text);
$comment->save();
$result = [
"status" => "success",
"message" => "Comment Saved"
];
}catch(Throwable $t){
$result = [
"status" => "error",
"message" => "Plz try again"
];
}
$result = [
"status" => "success",
"message" => ">Comment has been saved"
];
echo json_encode($result);
};
?>