Thursday, June 23, 2011

TUTORIAL COMMENT SYSTEM WITH ALLOW AND DENY PUBLISHING FUNCTIONALITY

first create table and call it "comment" with 4 fields id, name, comment and status mysql.php  <?php  @mysql_connect("localhost","root","") or die ("could not connect to mysql"); @mysql_select_db("name of your database") or die ("no database"); ?> ///////////page end here
index.php
<?php 
include_once "mysql.php";
//////render data that is already stored in the data base
 $sql_comments = mysql_query("SELECT * FROM comment ORDER BY id DESC");
while($row = mysql_fetch_array($sql_comments)){
$name = $row["name"];
$comment = $row["comment"];
$commentlist .= 'name : '.$name.'<br />comment : '.$comment.'<hr>';
}
//////////////
?>
<script type="text/javascript" src="js/jquery-1.4.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
 $('#loading').hide();
});
function wall(){
$('#loading').show();
  var name = $('#name').val();///#name is the input id from the form and will be equal the variable "name"
  var comment = $('#comment').val();/////#comment is textarea id from the form and will be equal the variable "comment"
  var URL = "post.php"; /////post.php will be equal the variable "comment"
  
  $.post(URL,{wall:"post",name1:name,comment1:comment},function(data){//parameter wall will be equal "post", name1 will be equal to the var "name" and comment1 will be equal to the var "comment"
  if($('#name').val() != "" && $('#comment').val() != ""){//if there is a value for name and comment
  $("#notice").show();//show the div with id = notice
  $("#notice").fadeOut(4000);//fade out the div with id = notice within 4 seconds
  }
  $('#loading').hide();
  var name = $('#name').val("");
  var comment = $('#comment').val("");
  });
 }
</script>
<table width="500" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td><div id='form'><form>
    name<br />
      <input type="text" id="name" />
      <br />
   comment<br />
      <textarea id="comment" cols="45" rows="5"></textarea>
      <br />
      <input type="submit" name="button" id="button" value="Submit" onclick="return false" onmousedown="javascript:wall();"/><img src="loading.gif" alt="" width="15" height="15" id="loading" />
      <br /></form>
    </div>
      <div id="result"><?php print"$commentlist";?></div></td>
  </tr>
</table>
post.php 
<?php
include_once "mysql.php";
  if ($_POST['wall'] == "post"){
  $name = $_POST['name1'];//name1 is parameter from the jquery function  
  $comment = $_POST['comment1'];//comment1 is parameter from the jquery function  
   if ($comment=="" || $name==""){exit();}// if no name or comment exit the script
   else{
   //insert the name and comment into database as pending 
  $sql = mysql_query("INSERT INTO comment (name,comment) 
     VALUES('$name','$comment','0')")
     or die (mysql_error());
  }
}
else if ($_POST['wall'] == "allow"){
   $comid = $_POST['cid'];
//set comment from pendded to published
   mysql_query("UPDATE comment SET status='1' WHERE id='$comid'") or die(mysql_error());
   echo"comment has been published.";
   }
   else if ($_POST['wall'] == "deny"){
   $comid = $_POST['cid'];
//delete comment if dennied
   mysql_query("DELETE FROM comment WHERE id='$comid'") or die(mysql_error());
   echo"comment has been deleted.";
   }
?>
admin.php
<?php 
include_once "mysql.php";
//////render data that is already stored in the data base
 $sql_comments = mysql_query("SELECT * FROM comment WHERE status='0' ORDER BY id DESC");
while($row = mysql_fetch_array($sql_comments)){
    $id = $row["id"];
$name = $row["name"];
$comment = $row["comment"];
$commentlist .= '<div id="'.$id.'"><table width="700" border="0" cellspacing="0" cellpadding="0">//we are going to the comment id as div id
  <tr>
    <td width="536" align="left" valign="top">name : '.$name.'<br />comment : '.$comment.'</td>
    <td width="150" align="left" valign="top">
<a id="comment'.$id.'" href="#" onclick="return false" onmousedown="javascript:allow('.$id.');">allow</a>
<a id="comment'.$id.'" href="#" onclick="return false" onmousedown="javascript:deny('.$id.');">deny</a></td>
  </tr>
</table></div><hr>';
}
//////////////
?>
<script type="text/javascript" src="js/jquery-1.4.2.js"></script>
<script type="text/javascript">
  function allow(id){//if admin want to publish
  
  var URL = 'post.php';
  $.post(URL,{wall:"allow",cid:id},function(data){
  $('#'+id).html(data);
  });
  }
  function deny(id){//if admin want to delete
  
  var URL='post.php';
  $.post(URL,{wall:"deny",cid:id},function(data){
  $('#'+id).html(data);
  });
  }
</script>
<style type="text/css">
<!--
.style {
font-size: 16px;
font-weight: bold;
font-family: Arial, Helvetica, sans-serif;
color: #333333;
}
-->
</style>
<span class="style">Pending comments</span>
<hr>
<?php print"$commentlist"; ?>

No comments:

Post a Comment