2011-07-30 72 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

相關問題