Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[JS] AJAX Comment System
#1
This is the bare Javascript version of my newest script.
  • Uses AJAX to submit comments
  • Dynamically shows new comments
  • Stores all content in database
  • Dates and IP logging with the database
  • Support for individual 'statuses' (each comment box is its seperate div, meaning no comment from one box will appear on another)
  • Unfinished dynamic comment box
See it live in action,
http://projectevolution.net76.net/tutorials/webdev/

index.php

PHP Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
html>
 <
head>
  <
title>Fun st00f</title>
  <
script type="text/Javascript">
 
   
// Can be used to dynamically create comment box - just like facebook
   // must be configured/fixed yourself
   
function showCommentBox(status_id) { 
    var 
status document.getElementById("status"); 
    var 
boxdiv document.createElement("div");
    var 
posterbox document.createElement("input");
    var 
label1 document.createElement("label");
    var 
label2 document.createElement("label");
    var 
commentbox document.createElement("input");
    var 
submit document.createElement("input");
    
boxdiv.setAttribute("id""comment");
    
posterbox.setAttribute("type""text");
    
posterbox.setAttribute("id""poster");
    
posterbox.setAttribute("name""poster");
    
commentbox.setAttribute("type""text");
    
commentbox.setAttribute("id""reply");
    
commentbox.setAttribute("name""reply");
    
submit.setAttribute("type""button");
    
submit.setAttribute("value""Comment");
    
submit.setAttribute("onclick""post("+status_id+")");
    
label1.setAttribute("for""poster");
    
label1.innerHTML "Name: ";
    
label1.setAttribute("for""reply");
    
label2.innerHTML "Reply: ";
    
boxdiv.appendChild(label1);
    
boxdiv.appendChild(posterbox);
    
boxdiv.appendChild(document.createElement("br"));
    
boxdiv.appendChild(label2);
    
boxdiv.appendChild(commentbox);
    
boxdiv.appendChild(document.createElement("br"));
    
boxdiv.appendChild(submit);
    
status.appendChild(boxdiv);
   }
 
   function 
formatTime(i) {
    if (
10)
       
"0" i;
    return 
i;
   }
 
   function 
post(status_id) {
 
      var 
comment document.getElementById("reply" status_id).value;
      var 
poster document.getElementById("poster" status_id).value;
 
      if (
comment == "" || poster == "") {
       
alert("You forgot to fill in the fields!");
       return;
      }
 
    if (
window.XMLHttpRequest) {
       
xmlhttp = new XMLHttpRequest();
      } else {
       
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
 
    
xmlhttp.onreadystatechange = function() {
       if (
xmlhttp.readyState == && xmlhttp.status == 200) {
          
document.getElementById("comments" status_id).innerHTML xmlhttp.responseText;
         }
      }
      var 
poster_ip "<?php echo $_SERVER['REMOTE_ADDR']; ?>";
    
xmlhttp.open("POST""post.php"true);
    
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    
xmlhttp.send("status_id="+status_id+"&poster="+poster+"&reply="+comment+"&posterip="+poster_ip);
   }
 
  </
script>
  <
style type="text/css">
 
   
#status {
    
width400px;
    
heightauto;
    
border1px solid gray;
    
margin-bottom20px;
   }
 
   
#status img {
    
floatleft;
    
padding5px;
   }
 
   
#status h4 {
    
floatleft;
    
margin-left15px;
   }
 
   
#status p {
    
border-top1px solid gray;
    
clearboth;
    
padding5px;
   }
 
   
#status a {
    
margin10px;
   }
 
   
#comment_poster {
    
widthauto;
    
padding5px;
    
backgroundrgb(234239244);
    
margin-top5px;
    
font-weightbold;
   }
 
   
#comment {
    
widthauto;
    
padding5px;
    
backgroundrgb(234239244);
   }
 
  </
style>
 </
head>
 
 <
body>
  <
h1>AJAX powered comment boxes system</h1>
 
  <?
php
 
   $conn 
mysql_connect('database servername''username''password');
   if (!
$conn)
    die(
'Unable to initiate connection! ' mysql_error());
   
mysql_select_db('db name');
 
   
$query "SELECT * FROM ajax_statuses";
   
$result mysql_query($query);
 
   
$input_id 1;
   while(
$row mysql_fetch_array($result)) {
    echo 
'
    <div id="status">
     <img src="http://profile.ak.fbcdn.net/hprofile-ak-snc4/hs466.snc4/49132_100001901520624_8137639_q.jpg" alt ="" />
     <h4>' 
$row['status_poster'] . '</h4>
     <p>' 
$row['status'] . '</p>
     <div id="comments'
.$input_id.'" style="margin: 10px;">';
 
     
$query "SELECT * FROM ajax_replies WHERE reply_id = " $row['status_id'] . " ORDER BY reply_date ASC";
     
$result2 mysql_query($query);
 
     while(
$row2 mysql_fetch_array($result2)) {
      echo 
'<div id="comment_poster">' $row2['reply_name'] . ' posted:</div>
      <div id="comment">' 
$row2['reply'] . ' - at: ' $row2['reply_date'] . '</div>';
     }
 
     echo 
'</div>
     <div id="comment">
      <label for="poster">Name: </label><input type="text" id="poster'
.$input_id.'" name="poster'.$input_id.'" /><br />
      <label for="reply">Reply: </label><input type="text" id="reply'
.$input_id.'" name="reply'.$input_id.'" /><br />
      <input type="button" value="Comment" onclick="post(' 
$row['status_id'] . ')" />
     </div>
    </div>'
;
 
    
$input_id++;
    
// <a href="javascript: showCommentBox(' . $row['status_id'] . ');" title="comment on this whore\'s status">Comment</a>
   
}
 
   
mysql_close($conn);
 
  
?>
 
 </body>
</html> 

post.php

PHP Code:
<?php
 $status_id 
$_POST['status_id'];
 
$poster htmlspecialchars($_POST['poster']);
 
$reply htmlspecialchars($_POST['reply']);
 
$poster_ip $_POST['posterip'];
 
$post_date date("Y-m-d H:i:s");
 
 
$conn mysql_connect('db servername''username''password');
 if (!
$conn)
  die(
'Unable to initiate connection! ' mysql_error());
 
mysql_select_db('db name');
 
 
$query "INSERT INTO ajax_replies VALUES ($status_id, '$reply', '$poster', '$poster_ip', '$post_date')";
 
$result mysql_query($query);
 if (!
$result)
  die(
mysql_error());
 
 
$query "SELECT * FROM ajax_replies WHERE reply_id = " $status_id " ORDER BY reply_date ASC";
 
$result mysql_query($query);
 
 while(
$row mysql_fetch_array($result)) {
  echo 
'<div id="comment_poster">' $row['reply_name'] . ' posted:</div>
   <div id="comment">' 
$row['reply'] . ' - at: ' $row['reply_date'] . '</div>';
 }
 
 
mysql_close($conn);
?>

tables.sql

Code:
CREATE TABLE ajax_statuses (
   `status_id` int NOT NULL AUTO_INCREMENT,
   `status` text NOT NULL,
   `status_poster` text NOT NULL,
   PRIMARY KEY (`status_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

CREATE TABLE ajax_replies (
   `reply_id` int NOT NULL,
   `reply` text NOT NULL,
   `reply_name` text NOT NULL,
   `reply_ip` text NOT NULL,
   `reply_date` datetime NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

I may make one jsut using jQuery another time.

Enjoy.
My SMF Modifications:
http://anthony.vibrantvps.com/smf
Reply
#2
Thank you for the share. Well designed script and no doubt very useful.
Reply
#3
This is super useful!
Rarely find a good AJAX tutorial
Thanks!
slowly getting better
Reply
#4
Good share :p
Reply
#5
Very handy snippet, thanks.
Reply
#6
Very nice script. I will observe this later on. Good job. Thumbsup
Reply
#7
Thanks everyone. Smile
My SMF Modifications:
http://anthony.vibrantvps.com/smf
Reply
#8
Nice share, will come in handy ;)! Thanks!
[Image: gviF7.gif]
"We've all heard that a million monkeys banging on a million typewriters
will eventually reproduce the entire works of Shakespeare.
Now, thanks to the Internet, we know this is not true." - Robert Wilensky
Reply
#9
nice share..........
Reply
#10
thnx dude, will try it out for sure ;P
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Release]PHP/AJAX Commenting Script Saint Michael 12 5,535 12-23-2010, 10:30 AM
Last Post: Buhrock
  [JS/jQuery] Implementing AJAX into your website Project Evolution 0 908 08-05-2010, 08:29 PM
Last Post: Project Evolution
  PHP ... and his friend AJAX. Spl3en 10 3,065 01-12-2010, 11:47 AM
Last Post: Spl3en

Forum Jump:


Users browsing this thread: 1 Guest(s)