2017-04-10 143 views
1

我使用AJAX爲一個抽搐視頻構建聊天系統。重點是動態顯示提交的消息,而無需重新加載頁面,並且工作正常。然而,這些消息並未提交給我的數據庫,並且在刷新頁面時,提交的消息不見了。顯示我手動插入數據庫中的數據。這裏是我的代碼:AJAX在數據庫中插入數據的問題

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(); 
}); 

DisplayCommentsModel: -

private $name; 
private $comment; 

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

    $cols = Array ("name", "comment"); 

    $comments = $app->db['db1']->get("comments", null, $cols); 
    if ($app->db['db1']->count > 0) 
    { 
     return $comments; 
    } 
} 

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:

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); 
} 
+0

如何獲得'$(「#forminput」)。serialize()'php'中的值 –

+0

你能更具體嗎?在php中獲取值是什麼意思? – CoffeeGuy

+0

@MayankPandeyz剛剛明白你的意思。感謝我的評論控制器 – CoffeeGuy

回答

1

在您的CommentsController文件中的post函數中,您沒有將數據插入到數據庫中。下面的代碼部分將只回應接收到的任何內容。

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

你應該叫insertCommentsInDb()在AddCommentsModel提供您發回的數據

1

$數據= json_encode($數據)之前; //不需要echo

$ this-> db-> insert('tableName',$ data); //你忘了這一行,這將插入json_format到數據庫

+0

你在哪裏建議我把「$ this-> db-> insert('tableName',$ data);」?在post()函數中不起作用。 – CoffeeGuy