Support Forums

Full Version: [Release]PHP/AJAX Commenting Script
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
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
.
well, I was going to look through the code and make a comment, but honestly, your code is very ugly and I really can't be bothered to read it
(12-18-2010, 09:41 AM)Orgy Wrote: [ -> ]well, I was going to look through the code and make a comment, but honestly, your code is very ugly and I really can't be bothered to read it
Okay? Explain to me how its ugly.
It's very packed together. It makes it difficult (well, annoying) to read. Your indentation is odd at some places, etc
(12-19-2010, 12:02 PM)Orgy Wrote: [ -> ]It's very packed together. It makes it difficult (well, annoying) to read. Your indentation is odd at some places, etc

I agree with Orgy on this, but OP, you still made something very nice and useful.
Oh okay, it just it makes it easier for me that's all. But thanks Orgy and Mike!
This is a really cool script!

I'm trying to learn AJAX
This isn't even AJAX, it's Jquery.
@Proof - Jquery has Ajax API included in the library.

@OP - Nice simple post, I prefer to not use Jquery to perform Ajax functions. Thats just me.
(12-21-2010, 07:13 PM)FiLTHY Wrote: [ -> ]@Proof - Jquery has Ajax API included in the library.

@OP - Nice simple post, I prefer to not use Jquery to perform Ajax functions. Thats just me.

Never the less, this isn't pure ajax.

Ajax =/= Jquery
Pages: 1 2