1

I'm using AJAX to build a chat system for a twitch video. The point is to dynamically display the submitted messages without the page being reloaded, and it works. However the messages aren't submitted to my Database and upon page refresh the submitted messages are gone. The ones that i manually inserted in the DB are displayed. Here's my code :

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);
}
5
  • How you are getting $("#forminput").serialize() values in php Commented Apr 10, 2017 at 6:17
  • Could you be more specific ? What do you mean by getting the values in php ? Commented Apr 10, 2017 at 6:18
  • @MayankPandeyz just understood what you mean. Its thanks to my Comments controller Commented Apr 10, 2017 at 6:25
  • Sure you've the right values in your variables? Try to display $data array somewhere before you insert it, just to be sure the values are in the array. Commented Apr 10, 2017 at 6:27
  • Added my ComentsController, its has what you need. Commented Apr 10, 2017 at 6:28

2 Answers 2

1

In post function in your CommentsController file, you are not inserting data into database. The code section below will just echo back whatever is received.

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

You should call insertCommentsInDb() available in AddCommentsModel before you send back the data

Sign up to request clarification or add additional context in comments.

Comments

1

$data=json_encode($data); // not need echo

$this->db->insert('tableName',$data); // You forgot this line and this will insert json_format to database

1 Comment

Where do you suggest i put the "$this->db->insert('tableName',$data); " ? Doesn't work in the post() function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.