2011-07-30 71 views
0
<style type="text/css">@import url("cstyle.css");</style> 
<style type="text/css">@import url("style.css");</style> 
<script type="text/javascript"> 
function deleteComment(id) 
{ 
    xmlhttp=new XMLHttpRequest(); 
    xmlhttp.open("GET","deleteComment.php?id="+id,true); 
    xmlhttp.send(); 
} 
$(document).ready(function() 
{ 
    deleteComment(id); 

}); 
</script> 
<?php 
include_once("pass.php"); 
include("common.php"); 

class ComA 
{ 
    static function reportComment() 
    { 
     $query = mysql_query("SELECT * FROM comments WHERE Flag =1"); 
     $number=mysql_num_rows($query); 
     // number of rows to show per page 
     $rowsperpage = 15; 
     // find out total pages 
     $totalpages = ceil($number/$rowsperpage); 
     // get the current page or set a default 
     if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) 
     { 
      // cast var as int 
      $currentpage = (int) $_GET['currentpage']; 
     } 
     else 
     { 
      // default page num 
      $currentpage = 1; 
     } 
     //if current page is greater than total pages... 
     if ($currentpage > $totalpages) 
     { 
      // set current page to last page 
      $currentpage = $totalpages; 
     } 
     //if current page is less than first page... 
     if ($currentpage < 1) 
     { 
      // set current page to first page 
      $currentpage = 1; 
     } 
     //the offset of the list, based on current page 
     $offset = ($currentpage - 1) * $rowsperpage; 
     // get the info from the db 
     // while there are rows to be fetched... 
     $sql = mysql_query("SELECT * FROM comments WHERE Flag = 1 ORDER BY id DESC LIMIT $offset, $rowsperpage"); 
     ?> 
     <div id="count"> 
     <div id="title"><b>Edit Reported Comments</b></div> 
     <?php 
     if($number==1) 
      echo $number . " COMMENT"; 
     else 
      echo $number . " COMMENTS"; 
     ?> 
     </div> 
     <?php 
     while ($content = mysql_fetch_assoc($sql)) 
     { 
      ?> 
      <div id="delete"> 
      <?php $javaid=$content['id']; ?> 
       <a href="<?php $_GET['location'] ?>" onClick="deleteComment(<?php echo $javaid ?>)" ><img src="_" onmouseover="_" onmouseout="_"></a> 
      </div> 
      <div id="comment"> 
      <?php 
      echo $content['username'] . " <br/> " ; 
      ?> 
      <div id="timestamp"> 
      <?php 
      echo "Posted " . $content['date'];  
      ?> 
      </div> 
      <?php 
      echo $content['comment']; 
      ?> 
      </div> 
      <div class="greyRule"><hr /></div> 
      <?php 
     } // end while 

     /****** build the pagination links ******/ 
     // range of num links to show 
     $range = 3; 
     ?> 
     <div id="page"> 
     <?php 
     // if not on page 1, don't show back links 
     if ($currentpage > 1) 
     { 
      ?> 
      <a href="<?php echo $_GET['location'] ?>&currentpage=1" class="blue"><< first </a> 
      <?php 
      // get previous page num 
      $prevpage = $currentpage - 1; 
      ?> 
      <a href="<?php echo $_GET['location'] ?>&currentpage=<?php echo $prevpage ?>" class="blue">< previous </a> 
      <?php 
     } 
     // loop to show links to range of pages around current page 
     for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) 
     { 
      // if it's a valid page number... 
      if (($x > 0) && ($x <= $totalpages)) 
      { 
       // if we're on current page... 
       if ($x == $currentpage) 
       { 
       // 'highlight' it but don't make a link 
       echo " <b>$x</b> "; //needs to be RIGHT ALIGNED 
       // if not current page... 
       } 
       else 
       { 
       // make it a link 
       ?> 
       <a href="<?php echo $_GET['location'] ?>&currentpage=<?php echo $x ?>" class="blue"> <?php echo $x?></a> 
       <?php 
       } // end else 
      } // end if 
     } // end for       
     // if not on last page, show forward and last page links   
     if ($currentpage != $totalpages && $totalpages!=0) 
     { 
      // get next page 
      $nextpage = $currentpage + 1;   
      ?> 
      <a href="<?php echo $_GET['location'] ?>&currentpage=<?php echo $nextpage ?>" class="blue"> next ></a> 
      <a href="<?php echo $_GET['location'] ?>&currentpage=<?php echo $totalpages ?>" class="blue"> last >></a> 
      <?php 
     } 
     ?> 
     </div> 
     <?php 
    } 
} 
ComA::reportComment(); 
?> 
+0

这个JavaScript作为卡莱d by onClick是一个ajax请求,php函数reportComment本身被另一个文件中的ajax调用。 –

回答

0

您正在以错误的方式使用XmlHttpRequest。更好的办法是使用这样的事情 - http://api.jquery.com/category/ajax/ 此外,您在页面加载调用deleteComment(),而不是点击链接

+0

这是被称为..... <?php include(“pass.php”)的函数。 级别删除 { \t静态函数deleteComment($ comId) \t { \t \t $ deleteComment =请求mysql_query( “DELETE FROM评论WHERE ID = '” $ comId。 “'”)或死亡(mysql_error); \t} } Delete :: deleteComment($ _ GET ['id']); ?> –

0

如果您正在使用jQuery,这是很简单的:

更改删除按钮对这样的事情:

<a class="delete_btn" rel="<?php echo $comment_id; ?>" href="#"><img src="_" onmouseover="_" onmouseout="_"></a> 

,并把这个脚本的地方在它下面:

<script> 
    $('.delete_btn').click(function() { 
    var id = $(this).attr('rel'); 
    $.get('deleteComment.php', {'id' : id}, function(data) { 
     $('#div_with_the_deleted_comment').html('This comment was deleted.'); 
     return false; 
    } 
    }); 
</script> 
+0

我试过这个,它没有工作。我添加了导入语句。我需要做其他事吗? –

+0

nvm我用一个更简单的解决方案解决了这个问题。我使用链接href直接调用该函数,然后重定向回该页面。感谢您的所有帮助。 –

+0

@Gavin Sellers很高兴能听到;)不客气! – Quasdunk

相关问题