include () блокирует PHP и AJAX - PullRequest
0 голосов
/ 21 мая 2019

У меня есть страница, которая должна передавать данные из JS AJAX в мою базу данных, но когда я использую

include("../bootstrap.php");

, больше ничего не работает.Если я пропущу это, JS работает, но ничего не помещается в базу данных.

JS + AJAX в индексе (работает нормально самостоятельно)

$(document).ready(function(){ 
            $("#submit").on("click", function(e){
                var text = $("#NewComment").val();

                $.ajax({
                    method: "POST",
                    url: "ajax/postcomment.php",
                    data: {text: text, postId: "<?php echo $postId ?>"},
                    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();
            });
        });

ajax / postcomment.php

<?php
    if(!empty($_POST)){

        $text = $_POST['text'];
        $postId = $_POST['postId'];

        include("../bootstrap.php"); //WEIRD ACTING INCLUDE/REQUIRE, PATH IS CORRECT

        try{
            $comment = new Comment();
            $comment->setText($text);
            $comment->setPostId($postId);
            $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);
    };

post.php с классом Comment (тоже отлично работает)

<?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"));
            return $statement->execute();
        }
    }
?>

Я не получаю никаких сообщений об ошибках, он просто ничего не делает с include ().Выполняет только JS, без PHP / SQL без него

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...