Log In

Email:
Password:
Keep me logged in

Forgot Password

Email:




Social Media
     
Archives
2013
March (7)
February (13)
January (14)
2012
December (12)
November (12)
October (12)
September (6)
August (12)
July (6)
June (12)
May (6)
April (12)
March (6)
February (12)
January (12)
2011
December (11)
November (14)
October (11)
September (4)
August (16)
July (8)
Categories
CSS (28)
jQuery (116)
PHP (52)
.htaccess (5)
iPod (1)
iPhone (16)
Sponsored Links


Comment System
PHP Ajax Javascript jQuery CSS
Browse Category | Friday, February 10, 2012 |

Description: Comment system that uses PHP & Ajax to post and delete comments, jQuery/Javascript to give its special effects, and CSS for the trash can icon.







MySQL:
Create tables for the comments and the content being commented.
CREATE TABLE `content` (
`id` INT NOT NULL ,
`content` VARCHAR( 50 ) NOT NULL
) ENGINE = MYISAM ;
CREATE TABLE `comments` (
`comment_id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`id` INT NOT NULL ,
`ip` VARCHAR( 50 ) NOT NULL ,
`username` VARCHAR( 50 ) NOT NULL ,
`comment` TEXT NOT NULL ,
`date_time` VARCHAR( 100 ) NOT NULL
) ENGINE = MYISAM ;
INSERT INTO content VALUES ('1','Demo'),('2','wcetdesigns.com');
PHP:
settings.php, file where all the important settings will be located and embedded in the other two files.
<?php

//MODIFY TO YOUR OWN SETTINGS

//CONNECTS TO YOUR DATABASE
$c mysql_connect("server""username""password");
$db mysql_select_db("your_database"$c);

//TABLES FOR THE CONTENT AND THE COMMENTS (MODIFY IF TABLE NAMES ARE DIFFERENT)
$content_table 'content';
$comment_table 'comments';

$ip $_SERVER["REMOTE_ADDR"]; //IP ADDRESS

?>
view.php, main file where all the functions will take place.
<?php

if(!$_GET["id"]){
    
header("Location: view.php?id=1");
}

$id $_GET["id"]; //ID OF THE CONTENT

?>
<html>
<head>
<script src="http://wcetdesigns.com/assets/javascript/jquery.js"></script>
<script>
$(function(){
$("#submit").click(function(){
var u = escape($('#u').val());
var c = escape($('#c').val());
var data = 'u='+u+'&c='+c+'&id=<?php echo $id; ?>';

$.ajax({
type: 'POST',
url: 'post-comment.php',
data: data,
success: function(e){
if(e){
if(!$('#error').is(':visible')){
$("#error").html(e).fadeIn(500).delay(2000).fadeOut(500);
}
} else {
if(!$('#posted').is(':visible')){
$("#posted").html("Comment posted").fadeIn(500).delay(2000).fadeOut(500);
}
$.get('view-comments.php?id=<?php echo $id; ?>', function(d){
$("#comment_posts").html(d);
});
$('#u, #c').val('');
}
}
});
});

$(".page").live("click", function(){
var id = $(this).attr("id");
$.get('view-comments.php?id=<?php echo $id; ?>&page='+id, function(d){
$("#comment_posts").html(d);
});
});
$('.comments').live("mouseover", function(){
var id = $(this).attr("id").substr(1);
$("#d"+id).show();
}).live("mouseout", function(){
var id = $(this).attr("id").substr(1);
$("#d"+id).hide();
});
$('.trash').live("click", function(){
var id = $(this).attr("id").substr(1);
var data = 'comment_id='+id;

$.ajax({
type: 'POST',
url: 'delete-comment.php',
data: data,
success: function(e){
if(e){
$("#error").html(e).fadeIn(500).delay(2000).fadeOut(500);
} else {
if(!$('#posted').is(':visible')){
$("#posted").html("Comment deleted").fadeIn(500).delay(2000).fadeOut(500);
}
$.get('view-comments.php?id=<?php echo $id; ?>', function(d){
$("#comment_posts").html(d);
});
}
}
});
});
});
</script>
<style>
#error {
color: #ed3232; display: none; font-size: 14px;
} #posted {
color: #32baed; display: none; font-size: 14px;
} table.comments {
border-left: 1px #ef89ff solid; border-right: 1px #ef89ff solid; border-bottom: 1px #ef89ff solid; position: relative; width: 300px;
} table.comments:first-child {
border-top: 1px #ef89ff solid;
} .page:hover {
cursor: pointer; text-decoration: underline;
} .trash {
display: none; position: absolute; right: 3px; bottom: 15px; width: 15px;
} .p {
background: #32baed;
} #tip {
height: 1px; left: 6px; position: absolute; top: -1px; width: 3px;
} #top {
height: 2px; width: 15px;
} #p1 {
height: 8px; left: 3px; position: absolute; -webkit-transform: rotate(355deg); top: 2px; width: 1px;
} #p2 {
height: 8px; left: 7px; position: absolute; top: 2px; width: 1px;
} #p3 {
height: 8px; right: 3px; position: absolute; -webkit-transform: rotate(5deg); top: 2px; width: 1px;
} #bottom {
top: 10px; height: 2px; left: 3px; position: absolute; width: 9px;
}
</style>
</head>
<body>
<?php

include("settings.php"); //FILE WITH IMPORTANT SETTINGS

$q mysql_query("SELECT * FROM $content_table WHERE id='$id'"); //FOR THE CONTENT
$t mysql_fetch_assoc($q);
$content $t["content"];
echo 
$content."<br><br>"//DISPLAYS CONTENT IN HTML

?>
<input id="u" placeholder="Name..."><br>
<textarea id="c" placeholder="Comment..."></textarea><br>
<input id="submit" type="button" value="Comment"><br>
<span id="error"></span>
<span id="posted"></span><br><br>
<div id="comment_posts">
<?php

include "view-comments.php";

?>
</div>
</body>
</html>
view-comments.php, file with the list of comments
<?php

include("settings.php"); //FILE WITH IMPORTANT SETTINGS

$id $_GET["id"]; //ID OF THE CONTENT

//PAGINATION PROPERTIES
$page 1;
if(
$_GET["page"]){
    
$page $_GET["page"];
}

$rrp 5//NUMBER OF COMMENTS PER PAGE
$o = ($page-1)*$rrp//FIRST COMMENT TO BE SHOWN

//QUERY FOR THE PAGE OF COMMENTS
$q mysql_query("SELECT * FROM $comment_table WHERE id='$id' ORDER BY comment_id DESC LIMIT $o, $rrp");

//THE PAGE OF COMMENTS
while($r=mysql_fetch_array($q)){
    
$c eregi_replace('\r\n''<br>'$r["comment"]);
    
$u $r["username"];
    
$date $r["date_time"];
    
    if(
$r["ip"]==$ip){
        
$delete '<div class="trash" id="d'.$r["comment_id"].'">
        <div class="p" id="top"></div>
        <div class="p" id="tip"></div>
        <div class="p" id="p1"></div>
        <div class="p" id="p2"></div>
        <div class="p" id="p3"></div>
        <div class="p" id="bottom"></div>
        </div>'
;
    }
    
    
$comments .= '<table class="comments" id="t'.$r["comment_id"].'"><tr><td><b>'.$u.'</b><br>'.$c.'<br><br><span style="color:#32baed; font-size:12px">'.$date.'</span>'.$delete.'</td></tr></table>';
}


//THE PAGE OF COMMENTS IN HTML
echo $comments;

//PAGINATION PROCESS
$q mysql_query("SELECT * FROM $comment_table WHERE id='$id'"); //FOR COMMENTS FOR THE SPECIFIC CONTENT
$n mysql_num_rows($q); //NUMBER OF COMMENTS
$pages ceil($n/$rrp); //NUMBER OF PAGES
$range 2//NUMBER OF PAGES TO BE LINKED BEFORE & AFTER THE CURRENTE PAGE NUMBER Ex: If page number is 5 (3, 4, 5, 6, 7)

//IF THERE IS MORE THAN 1 PAGE OF COMMENTS
if($pages>1){
    
//FIRST, PREVIOUS, NEXT, & LAST PAGE LINKS
    
if($page>1){
        
$first '<a class="page" id="1">First</a> ';
        
$prev '<a class="page" id="'.($page-1).'">Prev</a> ';
    }
    if(
$page<$pages){
        
$next '<a class="page" id="'.($page+1).'">Next</a> ';
        
$last '<a class="page" id="'.$pages.'">Last</a> ';
    }
    
//NUMBERIC PAGINATION (1, 2, 3, 4, 5, etc.)
    
for($p=($page-$range); $p<=($page+$range); $p++){
        if(
$p>=1&&$p<=$pages){
            if(
$p==$page){
                
$nav .= '<span style="color: #32baed">'.$p.'</span> '//DOES NOT LINK THE CURRENT PAGE NUMBER
            
} else {
                
$nav .= '<a class="page" id="'.$p.'">'.$p.'</a> ';
            }
        }
    }
}

//DISPLAYS THE PAGINATION IN HTML
echo $first $prev $nav $next $last;

?>
post-comment.php, ajax file where comment posts would be processed
<?php

include("settings.php"); //FILE WITH IMPORTANT SETTINGS

$id $_POST["id"]; //ID OF THE CONTENT
$username $_POST["u"];
$comment $_POST["c"];
$dateposted date("D, M j, Y") .' at '.date("h:i A");

//COUNTS THE NUMBER OF SPACE IN BOTH THE USERNAME AND THE COMMENT
$sc substr_count($comment" ");
$su substr_count($username" ");

//ONLY POSTS COMMENTS IF BOTH THE USERNAME AND THE COMMENT ARE NOT EMPTY
if($sc<strlen($comment)){
    if(
$su<strlen($username)){
        
mysql_query("INSERT INTO $comment_table VALUES('','$id','$ip','$username','$comment','$dateposted')");
    } else {
        echo 
'Please enter a name';
    }
} else {
    echo 
'Comment cannot be blank';
}

?>
delete-comment.php, ajax file where comments would be deleted
<?php

include("settings.php"); //FILE WITH IMPORTANT SETTINGS

$comment_id $_POST["comment_id"];

//CHECKS IF THE COMMENT STILL EXISTS IN THE DATABASE
$q =  mysql_query("SELECT * FROM $comment_table WHERE comment_id='$comment_id' AND ip='$ip'");
$n mysql_num_rows($q);

//IF SO, THE COMMENT WILL BE DELETED
if($n){
    
mysql_query("DELETE FROM $comment_table WHERE comment_id='$comment_id' AND ip='$ip'");
} else {
    echo 
'Error deleting comment';
}

?>
 22    10
Views: 4,412 Downloads: 627




(Optional in case you want a reply)



87 Comments
eretwerf
wretregtsdfvsfd

Sunday, April 21, 2013 at 11:06:49 PM
fdfdfdf
hello

Friday, March 8, 2013 at 2:42:53 PM
wooot
stop me

Sunday, Febuary 10, 2013 at 1:05:04 PM
wooot
duh

Sunday, Febuary 10, 2013 at 1:04:56 PM
wooot
muh

Sunday, Febuary 10, 2013 at 1:04:50 PM
1 2 3 4 5 Next Last