Php AJAX在数据库中插入数据时出现问题

Php AJAX在数据库中插入数据时出现问题,php,ajax,Php,Ajax,我正在使用AJAX为twitch视频构建一个聊天系统。关键是动态显示提交的消息,而不需要重新加载页面,并且可以正常工作。但是,这些消息不会提交到我的数据库,页面刷新后,提交的消息将消失。将显示我手动插入数据库中的内容。这是我的密码: Dashboard.twig: <script> $("#forminput").submit(function(e){ var url = "dashboard"; $.ajax({ type: "POST",

我正在使用AJAX为twitch视频构建一个聊天系统。关键是动态显示提交的消息,而不需要重新加载页面,并且可以正常工作。但是,这些消息不会提交到我的数据库,页面刷新后,提交的消息将消失。将显示我手动插入数据库中的内容。这是我的密码:

Dashboard.twig:

<script>

$("#forminput").submit(function(e){
    var url = "dashboard";
    $.ajax({
        type: "POST",
        url: url,
        data: $("#forminput").serialize(),
        dataType: 'json', //what is returned
        success : function(data)
        {
            $("#table").append("<tr><td>" + data.name + "</td><td>" + data.comment + "</td></tr>");
        }

    });
    e.preventDefault();
});
AddCommentsModel

private $name;
private $comment;

public function __construct($name, $comment)
{
    $this->name = $name;
    $this->comment = $comment;
}

public function comment()
{
    if ($this->validateEmptyFields() == false)
    {
        return false;
    }
    if ($this->validateFilledFields() == false)
    {
        return false;
    }
    return true;
}

public function validateEmptyFields()
{
    if(empty($this->name) || empty($this->comment))
    {
        return false;
    }
    else
    {
        return true;
    }

}

public function validateFilledFields()
{
    $nameLenght = strlen($this->name);
    $commentLenght = strlen($this->comment);

    if($nameLenght < 2 && $commentLenght < 2)
    {
        return false;
    } 
    return true;
}

public function insertCommentsInDb()
{
    $app = \Yee\Yee::getInstance();

    $data = array(
        "name" => $this->name,
        "comment" => $this->comment
        );

    $app->db['db1']->insert('comments', $data);
}

在CommentsController文件的post函数中,您并没有将数据插入数据库。下面的代码部分只回显接收到的任何内容

$data = array(
        'name' => $name,
        'comment' => $comment
    );
echo json_encode($data);
在发回数据之前,应该调用AddCommentsModel中提供的insertCommentsInDb()。

$data=json\u encode($data);//不需要回声

$this->db->insert('tableName',$data);
//您忘记了这一行,这将在数据库中插入json_格式

如何获取
$(“#forminput”)。请在
php
中序列化()?你用php获取值是什么意思?@MayankPandeyz刚刚理解你的意思。多亏了我的评论,你的变量值正确吗?在插入数组之前,请尝试在某处显示
$data
数组,以确保数组中的值。添加了我的ComentsController,它具有您所需的功能。您建议我将“$this->db->insert('tableName',$data);”放在哪里?在post()函数中不起作用。
public function index()
{
    $app = $this->getYee();
    $newDisplayCommentsModel = new DisplayCommentsModel();
    $comments = $newDisplayCommentsModel->printComments();
    $data = array(
        'comments' => $comments
    );
    $app->render('dashboard/dashboard.twig', $data);
}

/**
 * @Route('/dashboard')
 * @Name('dashboard.post')
 * @Method('POST')
 */
public function post()
{
    $app = $this->getYee();

    $name = $app->request->post('name');
    $comment = $app->request->post('comment');

    //add to database

    $data = array(
        'name' => $name,
        'comment' => $comment
    );

    echo json_encode($data);
}
$data = array(
        'name' => $name,
        'comment' => $comment
    );
echo json_encode($data);