Alice
Alice

Reputation: 424

AJAX Comments with Jquery

I own a flash game website and I want my users to be able to post comments without having the page refreshed. Could someone give me an example?

function addcomment($uid, $gid, $text){
    connect();
    $date = date("Y-m-d H:i:s");
    mysql_query("INSERT INTO `comments` (`uid`, `gid`, `text`, `date`) VALUES ($uid, $gid, '$text', '$date'");
}

Upvotes: 0

Views: 536

Answers (2)

mario
mario

Reputation: 145482

Submitting a form via jQuery is often as simple as

<input type=submit value="submit"
 onClick="$.post('save_comment.php', $('form#example').serialize())">

This triggers an ordinary POST request, and in your save_comment.php script you just add:

<?php
  addcomment($_SESSION["uid"], $_POST["gid"], $_POST["comment_field"]);

Note that you need mysql_real_escape_query() around the variables here - or in your addcomment() function (where it belongs).

Add a jQuery success: callback if your comment saving script returns something that you want to inject into the html page (e.g. formatted comment or error message string).

Upvotes: 1

bluefoot
bluefoot

Reputation: 10580

First of all, you should not put SQL code in your view pages.

Try to use this plugin to manage the ajax form submission.

Upvotes: 1

Related Questions