12-16-2010, 07:18 PM
PHP/AJAX Commenting Script
By: Saint Michael
Hello SF, I have been working on this script for a couple days now, basically its a commenting script. It is a script that allows a user to add a comment to what ever you post. So, let me explain how to use this script, first go into your Admin CP provided by your web host, then navigate to PhpMyAdmin and make a table called comments. Now, add 3 fields, ID - set AUTO_INCREMENT to 1, and set it to the primary key, author - Set this to TEXT, message - Set this to TEXT as well. Then we just modify the script a little,
PHP Code:
<?php
/************************************************************
* PHP/AJAX Comenting Script By: Saint Michael *
* 10/14/10, Copyright SaintMichael Scripts *
* http://www.gnu.org/licenses/gpl.html *
* Redistribution is allowed under authors extent. *
*************************************************************/
mysql_connect("localhost","user","pass"); //Remember to change to your DB's Information
mysql_select_db("DataBase"); //Change to your Database
if($_POST['type'] == "addcomment") {
$author = mysql_real_escape_string($_POST['author']);
$message = mysql_real_escape_string($_POST['message']);
//The mysql_real_escape_string is to prevent sql injection
if($author == "" || $message == "") {
die("<p><font color='red'>Error: Please fill out BOTH fields.</font></p>");
//gives an error message if the user doesnt fill in both feilds.
}
$q1 = mysql_query("INSERT INTO `comments` (`author`, `message`) VALUES ('$author', '$message')") or die("<p>Mysql Error: <b>".mysql_error()."</b></p>"); //Prints the mysql error
echo "<p><font color='green'>Comment added successfully. You should see it in a few seconds.</font></p>";
} elseif($_POST['type'] =="getcomments") {
$q1 = mysql_query("SELECT * FROM `comments`");
$n1 = mysql_num_rows($q1);
if($n1 == 0) {
die("<p><font color='red'>No Comments were found.</font></p>");
}
echo "<table border=1>";
echo "<tr><td><b>Author </b></td><td><b>Comment</b></td></tr>";
while($r1 = mysql_fetch_assoc($q1)) {
$a = $r1['author'];
$m = $r1['message'];
echo "<tr><td>$a</td><td>$m</td></tr>";
}
echo "</table>";
} else {
?>
You should now what to change, but anyways continuing, now for the actual AJAX/HTML part of this.
Code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Ajax Commenting Script By SaintMichael</title>
<script src="http://code.jquery.com/jquery-1.4.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
(document).ready(funtction(){
$("submit").click(function(){
var user = $("#author").val();
var comments = $("#message").val();
$.post("comments.php",{type: "addcomment",author: user,message: comment},function(data){$("#return").html(data)});
return false;
});
});
function getComments() {
$.post("comments.php",{type: "getcomments"},function(data){$("#comments").hmtl(data)});
}
setInterval("getComments()",10000);
</script>
</head>
<body>
<div style="font-size: 18px;">
<p>Current Comments:</p>
<div id ="comments"></div><!--This div will be filled with AJAX-->
<hr />
<p>Add a comment:</p>
<form action="" method="post">
<p>Username: <input type="text" name="author" id="author" /></p>
<p>Comment: <textarea name="message" cols="70" rows="10" id="submit" /><p>
<p><input type="submit" name="submit" value="Add Comment" id="submit" /></p>
</form>
<div id="return"></div> <!--This div will be filled with return data-->
</div>
</body>
</html>
<? }
//end the if at the top of the page
?>
</html>
-Saint Michael
P.s. Thanks to TheNewBoston for providing the basic commenting script.