Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Release]PHP/AJAX Commenting Script
#1
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 {
?>
REMINDER: SAVE AS "comments.php" WITH OUT THE QUOTATION MARKS.
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>
Just add the Header to your header, and add the body to your body after your "Content." And as Buzz would say DONT RIP, or ill Rip you.
-Saint Michael
P.s. Thanks to TheNewBoston for providing the basic commenting script
.
[Image: angelsig.jpg]


Reply


Messages In This Thread
[Release]PHP/AJAX Commenting Script - by Saint Michael - 12-16-2010, 07:18 PM
RE: [Release]PHP/AJAX Commenting Script - by Orgy - 12-18-2010, 09:41 AM
RE: [Release]PHP/AJAX Commenting Script - by Orgy - 12-19-2010, 12:02 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  PHP Framework List: An Ultimate Guide to 102 PHP Frameworks for Web Developers tk-hassan 0 941 07-27-2020, 11:26 PM
Last Post: tk-hassan
  Req. Upload PHP script. 3 X P L 0 I T 1 1,334 07-14-2012, 05:19 PM
Last Post: 'Snorlax
  PHP Video Tutorials (PHP For Beginners) Eleqtriq 4 3,666 10-10-2011, 01:00 PM
Last Post: Greyersting
  optimize mysql through a php script andrewjs18 7 3,345 10-05-2011, 06:31 PM
Last Post: John.
  [JS] AJAX Comment System Project Evolution 13 6,004 05-14-2011, 02:18 AM
Last Post: モrainee

Forum Jump:


Users browsing this thread: 4 Guest(s)