小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

實(shí)現(xiàn)一個(gè)無(wú)刷新的基于ajax的簡(jiǎn)易聊天室

 昵稱2807 2006-10-12
 

實(shí)現(xiàn)一個(gè)基于ajax的簡(jiǎn)易聊天室

1 程序主頁(yè)面

<html>
<head>
<title>在線交流對(duì)話</title>
<META http-equiv=Content-Language content=zh-cn>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<?php
 include_once("server1.server.php"); #servidor para XAJAX
 $xajax->printJavascript();
?>

</head>
<body>
<div id=display>
 
</div>

<script language=javascript>
function reloaddisplay()
{
xajax_reloaddisplay();
}
setInterval(reloaddisplay,5000); //五秒鐘自動(dòng)讀取最新的留言15條
</script>

<script language=javascript>
    function onSubmit() {
      xajax_speak(xajax.getFormValues("speakForm"));
    }
</script>

<div id=speak>
<form id="speakForm" action="javascript:void(null);" onsubmit="onSubmit();">
  <fieldset>
  <legend>Please into your words here!</legend>
  <label>Speak:</label>
  <input type="text" name="words" id="words" size="40" maxlength="150" />
  <input type="submit" value="enter"> //點(diǎn)擊按鈕時(shí)提交
  <input type="hidden" name="nick" id="nick" value="guest" />
  </fieldset>
</form>
</div>

</body>
</html>

2 腳本

-- 聊天室
CREATE TABLE TBL_CHAT (
  nick varchar(10) NOT NULL default ‘‘,
  words varchar(150) NOT NULL default ‘‘,
  chattime bigint(20) unsigned NOT NULL default ‘0‘
) TYPE=MyISAM;

3 服務(wù)器端

<?php
include ‘include.php‘;
require("xajax.inc.php");

//extend response
class myXajaxResponse extends xajaxResponse

  function addAddOption($sSelectId, $sOptionText, $sOptionValue) 
  { 
    $sScript  = "var objOption = new Option(‘".$sOptionText."‘,‘".$sOptionValue."‘);";
    $sScript .= "document.getElementById(‘".$sSelectId."‘).options.add(objOption);";
    $this->addScript($sScript);
  }
 
  function addClearSelect($sSelectId) 
  { 
    $sScript .= "document.getElementById(‘".$sSelectId."‘).options.length = 0;";
    $sScript .= "document.getElementById(‘".$sSelectId."‘).options.selectedIndex = 0;";
    $this->addScript($sScript);
  }
}

//顯示留言的函數(shù)

function reloaddisplay(){
  global $db,$now;
 
 $objResponse = new xajaxResponse();
 $tmp="";
 
  $chats = $db->getAll($db->modifyLimitQuery("select * from ".TBL_CHAT." order by chattime desc", 0, 15));
  for ($i = 0, $count = count($chats); $i < $count; $i++) {
   $tmp.= $chats[$i][‘nick‘].":".$chats[$i][‘words‘]."<br>";
  }
 
  //Delete old data 只保留15條信息
  $nr = $db->getOne("select count(*) from ".TBL_CHAT);
  if($nr > 15) {
   $delnr = $nr - 15;
    $db->query("delete from TBL_CHAT order by chattime asc limit $delnr)");
  } 

 $objResponse->addAssign("display","innerHTML",$tmp);
 return $objResponse->getXML();

}

//無(wú)提交表單程序,特別注意這里需要用iconv還原url的編碼

function speak($formData){
 global $db,$now;
  $objResponse = new xajaxResponse();
 
  $words = iconv("UTF-8","GBK",$formData[‘words‘]);
  $nick = iconv("UTF-8","GBK",$formData[‘nick‘]);
 
  if(strlen($words) > 0 && strlen($nick) > 0) {
   $db->query("insert into ".TBL_CHAT." (nick, words,chattime) values ("
                  .$db->quote(stripslashes($nick)).", "
                  .$db->quote(stripslashes($words))
                  .", ‘$now‘)");                 
  }
  $objResponse->addClear("words","value");

  return $objResponse->getXML();
}


$xajax = new xajax("server1.server.php","xajax_",false);
$xajax->registerFunction("reloaddisplay");
$xajax->registerFunction("speak");
$xajax->processRequests();
?>

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多