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
#2
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
[Image: TYzKF.png]
Reply
#3
(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.
[Image: angelsig.jpg]


Reply
#4
It's very packed together. It makes it difficult (well, annoying) to read. Your indentation is odd at some places, etc
[Image: TYzKF.png]
Reply
#5
(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.
Ninja
Reply
#6
Oh okay, it just it makes it easier for me that's all. But thanks Orgy and Mike!
[Image: angelsig.jpg]


Reply
#7
This is a really cool script!

I'm trying to learn AJAX
Reply
#8
This isn't even AJAX, it's Jquery.
(11-23-2010, 08:40 PM)Guest Wrote: Alright, did the 'Support' get removed from SupportForums?
You're not a good Emotional Helper.
Reply
#9
@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.
Reply
#10
(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
(11-23-2010, 08:40 PM)Guest Wrote: Alright, did the 'Support' get removed from SupportForums?
You're not a good Emotional Helper.
Reply


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,335 07-14-2012, 05:19 PM
Last Post: 'Snorlax
  PHP Video Tutorials (PHP For Beginners) Eleqtriq 4 3,667 10-10-2011, 01:00 PM
Last Post: Greyersting
  optimize mysql through a php script andrewjs18 7 3,348 10-05-2011, 06:31 PM
Last Post: John.
  [JS] AJAX Comment System Project Evolution 13 6,012 05-14-2011, 02:18 AM
Last Post: モrainee

Forum Jump:


Users browsing this thread: 1 Guest(s)