jquery+ajax+xml+php簡單留言板 - jQuery - 。

來源:互聯網
上載者:User

示範地址 http://www.cnjquery.com/demo/gb/guestbook.html

話不多說,如果哦看不動的去論壇上發帖

guestbook.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>無刷jquery php xml 留言板版</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style>
* { margin:0; padding:0}
ul {list-style:none}
#msglist{width:800px; vertical-align:text-top; margin:20px auto 0px auto; }

dt{background:url(wtp-m.png);padding:5px}
dl{border:solid 1px #8db2e3}
dd{padding:5px}
#page span{cursor: pointer;}
#commentformbox{width:800px;vertical-align:text-top;margin:20px auto 0px auto; }
#commentformbox TEXTAREA{width:400px;height:100px;vertical-align:text-top;margin:20px auto 0px auto; }
</style>
<SCRIPT LANGUAGE="JavaScript" src="js/jquery-1.2.6.js"></script>
<SCRIPT LANGUAGE="JavaScript">
<!--
$(document).ready(function() {

function load_msg(page)
{
     $('#state').html("目前狀態:正在載入資料請稍等....");
    page=$(this).html();
    page=page=="undefined"?1:page;
 
 $.post("comments-ajax.php",{action:"load",page:page},function(data){
 //alert($(data).find("span").text());
 
   $('#commentlist dl').html(data); // 追加留言資料
   $('#commentlist dl').find("span").bind("click",load_msg);
     $('#state').html("目前狀態:資料載入完畢");
 })
 
}

load_msg();
 if ($('#commentform').length) {
  $('#commentform').submit(function(){
   jQuery.ajax({
    url: 'comments-ajax.php', // 這裡要改為 comments-ajax.php 檔案的位置
    data: $('#commentform').serialize(), // 從表單中擷取資料
    type: 'POST', // 佈建要求類型為 ‘POST’,預設為 ‘GET’
    beforeSend: function() {
     $('#state').html("目前狀態:正在發送資料。。。。");
    },
    error: function(request) {
     $('#state').html(request.responseText);
    },
    success: function(data) {
     $('textarea').each(function(){
      this.value='';
     });
     
     $('#state').html("目前狀態:發布資料成功");
     $('#commentlist dl').prepend(data); // 追加留言資料
     $('#commentform :input').attr('disabled', true);
     $('#commentformbox').fadeOut(1000);
     $('#commentload').hide();
     setTimeout(function() { // 提交留言 15 秒後方可再次提交新留言
      $('#commentform :input').removeAttr('disabled');
      $('#commentformbox').fadeIn(15000);
     }, 5000);
    }
   });
   return false;
  });
 }
})

//-->
</SCRIPT>
<body>
<div ID="state">目前狀態</div>
<div ID="msglist">
 <div id="commentlist">
   <dl>
    
   </dl>
 </div>
</div>

<div id=commentformbox>
<FORM METHOD=POST ACTION="" ID="commentform">
 <TEXTAREA NAME="Content" ROWS="" COLS=""></TEXTAREA><br><INPUT TYPE="submit" value="發布資訊">
</FORM>
</div>
</body>

comments-ajax.php

<?php
//==========================================
// 檔案名稱: comments-ajax.php
// 說   明: 簡單的jquery php XML 留言簿
// power by cnjquery.com
//==========================================
if ($_SERVER["REQUEST_METHOD"] != "POST") {
    header('Allow: POST');
    header("HTTP/1.1 405 Method Not Allowed");
    header("Content-type: text/plain");
    exit;
}

class guestbook extends DOMDocument
{  
 var $pagenum=10;

 public function __construct()
 {
    parent:: __construct();
    if (!file_exists("guestbook.xml"))
    {
  $xmlstr = "<?xml version='1.0'?><msglist></msglist>";
  $this->loadXML($xmlstr);
  $this->save("guestbook.xml");
    }
    else
  $this->load("guestbook.xml");
 }
    public function loadMessage($page)
 {
   $page=($page>0)?$page:1;
   $num=($page-1)*$this->pagenum;
   $roots = $this->getElementsByTagName( "msglist" );
   //print_r($roots);
   $root=$roots->item(0);
   $msg=$root->getElementsByTagName( "msg" );
   $total=$msg->length;
  // echo $total;
   $totalpage=ceil($total/$this->pagenum);
   $totalpage=$totalpage?$totalpage:1;
   $startid=$total<$num?$total:$total-$num;
   $listnum=$startid>$this->pagenum?$startid-$this->pagenum:0;
   //echo $startid;
   //echo $listnum;
    for($i=$startid-1;$i>=$listnum;$i--)
  {
 
   echo "<dt> &nbsp;&nbsp;ip:".$msg->item($i)->getElementsByTagName("ip")->item(0)->nodeValue."&nbsp;&nbsp;time:".$msg->item($i)->getElementsByTagName("time")->item(0)->nodeValue."</dt><dd>".trim(nl2br($msg->item($i)->getElementsByTagName("content")->item(0)->nodeValue))."</dd>";
  }
    echo "<dt ID=page>";
    for($i=1;$i<=$totalpage;$i++)
  {
     echo "&nbsp;&nbsp;<span>".$i."</span>";
  }
        echo "</dt>";

 }
 public function saveMessage($clientIP,$clientTime,$postContent)
 {
     $msglists = $this->getElementsByTagName( "msglist" );
     $msglist = $msglists->item(0);
     $msg = $this->createElement( "msg" );
     $ip = $this->createElement( "ip" );
     $ip->appendChild( $this->createTextNode($clientIP)  );
     $msg->appendChild($ip);
     $time=$this->createElement( "time" );
     $time->appendChild( $this->createTextNode($clientTime)  );
     $msg->appendChild($time);
     $content=$this->createElement( "content" );
     $content->appendChild( $this->createCDATASection(nl2br(SafeHtml($_POST["Content"])))  );
     $msg->appendChild($content);
     $msglist->insertBefore ($msg);
     $this->save("guestbook.xml");
    echo "<dt> &nbsp;&nbsp;ip:".$clientIP."&nbsp;&nbsp;time:".$clientTime."</dt><dd>".$postContent."</dd>";
 }
}
$guestbook = new guestbook;
if($_POST["action"]=="load")
{
 $guestbook->loadMessage(intval($_POST["page"]));
 exit;
}
$IP=$_SERVER["SERVER_ADDR"];
$time=date("Y-m-d H:i:s");
$content=nl2br(SafeHtml($_POST["Content"]));
$guestbook->saveMessage($IP,$time,$content);
exit;

//http://hellobmw.com/archives/ajax-comments-with-jquery-for-wordpress.html
Function SafeHtml($msg = "",$clear_script=true)
{
 if(empty($msg))
 {
  return false;
 }
 $msg = str_replace('&amp;','&',$msg);
 $msg = str_replace('&nbsp;',' ',$msg);
 if(get_magic_quotes_gpc())
 {
  $msg = str_replace("'","&#39;",$msg);
  $msg = str_replace('"',"&quot;",$msg);
 }
 else
 {
  $msg = str_replace("'","&#39;",$msg);
  $msg = str_replace('"',"&quot;",$msg);
 }
 $msg = str_replace("<","&lt;",$msg);
 $msg = str_replace(">","&gt;",$msg);
 $msg = str_replace(" ","&nbsp; &nbsp; ",$msg);
 $msg = str_replace(" ","",$msg);
 $msg = str_replace("   ","&nbsp; &nbsp;",$msg);
 if($clear_script)
 {
  $msg = preg_replace("/<script(.*?)</script>/is","",$msg);
  $msg = preg_replace("/<frame(.*?)>/is","",$msg);
  $msg = preg_replace("/<iframe(.*?)>/is","",$msg);
  $msg = preg_replace("/</fram(.*?)>/is","",$msg);
 }
 return $msg;
}

?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.