【bf88必发唯一官网】php无刷新回贴和注册查证实例,ajax验证用户名完成代码

by admin on 2019年8月8日

本文实例汇报了PHP+mysql+ajax轻量级聊天室实现格局。共享给大家供大家参照他事他说加以考察,具体如下:
做了三个QQ聊天交友网址,想加个聊天的意义,于是做完用PHP做了简单又有力的聊天室

做了三个QQ聊天交友网址,想加个聊天的成效,于是做完用PHP做了轻巧又有力的聊天室

 

先看xin.sql数据库,大家可径直复制保存成xxx.sql哦。

  1. 创建mysql数据库表:

1.
创建mysql数据库表:create
table chat( id bigint AUTO_INCREMENT,username varchar(20), chatdate
datetime,msg varchar(500), primary key(id));

<html>
<head>
<meta http-equiv=”content-type” content=”text/html;
charset=gb2312″>
<title>ajax用户验证</title>
<style>
body {
 font: 12px , arial, helvetica, sans-serif; color: #808080
}
#reg{
 width: 600px;
 border: 1px dotted #336699;
}
td{font-size:12px;color:}
h1{
 height:35px;
 font-size:16px;
 color:#ffffff;
 background-color: #336699;
 line-height: 30px;
 padding-left:25px;
}

.anniu {
 border-right: #666666 1px solid; padding-right: 1px; border-top:
#ffffff 1px solid; padding-left: 1px; font-size: 9pt; padding-bottom:
1px; border-left: #ffffff 1px solid; cursor: hand; color: #333333;
padding-top: 1px; border-bottom: #666666 1px solid; font-family:
“宋体”; height: 20px; background-color: #eeeeee
}
.framedivpass{
 
 border: 1px solid #6fbe44;
 padding: 2px;
 height: 100%;
 float: left;
}
.framedivwarn{
 
 border: 1px solid #ff0000;
 padding: 2px;
 height: 100%;
 float: left;
}
</style>
<script language=”网页特效”
type=”text/javascript”>
 var xmlhttp = false;
 try {
  xmlhttp = new activexobject(“msxml2.xmlhttp”);
 } catch (e) {
  try {
   xmlhttp = new activexobject(“microsoft.xmlhttp”);
  } catch (e2) {
   xmlhttp = false;
  }
 }
 if (!xmlhttp && typeof xmlhttprequest != ‘undefined’) {
  xmlhttp = new xmlhttprequest();
 }
 function ajax(data){ 
  xmlhttp.open(“get”,”user_ck.php教程?username=”+document.getelementbyid(“username”).value,true);
  xmlhttp.send(null);
     document.getelementbyid(‘username_notice’).innerhtml =
process_request;//显示状态
  xmlhttp.onreadystatechange=function(){
   if (4==xmlhttp.readystate){
    if (200==xmlhttp.status){
    var responsetext = xmlhttp.responsetext;
       if (responsetext==”true” ){
       ck_user(“true”);
          }
       else{
       ck_user(“false”);
       }
    }else{
     alert(“产生错误!”);
    }
   }
  }
 }
 function chkusername(obj){
      if (checks(obj.value)== false)
    {
   obj.classname = “framedivwarn”;
   showinfo(“username_notice”,msg_un_format);
            change_submit(“true”);
    }
  else if(obj.value.length<1){
   obj.classname = “framedivwarn”;
   showinfo(“username_notice”,msg_un_blank);
            change_submit(“true”);
  }

  else if(obj.value.length<3){
   obj.classname = “framedivwarn”;
   showinfo(“username_notice”,username_shorter);
            change_submit(“true”);
  }
  else{
   //调用ajax函数,向劳动器端发送查询
   ajax(obj.value);
  }   

 }

 代码如下

create table chat( id bigint AUTO_INCREMENT,username varchar(20),
chatdate datetime,msg varchar(500), primary key(id));
2.编辑建议连接数据库函数:
dbconnect.php

2.编写制定建议连接数据库函数:

 

use xin;
CREATE TABLE bbs_post (
  id int(11) NOT NULL auto_increment,
  title varchar(255) NOT NULL,
  username varchar(255) NOT NULL,
  content varchar(255) NOT NULL,
  threadid int(11) NOT NULL,
  PRIMARY KEY  (id)
);

<?php
function db_connect()
{
 date_default_timezone_set(“Asia/Shanghai”);
 $link = mysql_connect(“xxx.xxx.xxx.xxx”, “databasename”, “password”)
      or die(‘不可能连接: ‘ . mysql_【bf88必发唯一官网】php无刷新回贴和注册查证实例,ajax验证用户名完成代码。error());
 mysql_select_db(“databasename”)
or die(‘没有你找到钦命数量库’);
 return true;
}
function quote($strText)
{
  $Mstr = addslashes($strText);
  return “‘” . $Mstr . “‘”;
}
function isdate($d)
{
  $ret = true;
  try
  {
    $x = date(“d”,$d);
  }
  catch (Exception $e)
  {
    $ret = false;
  }
  echo $x;
  return $ret;
}
?>

dbconnect.php

 

INSERT INTO bbs_post VALUES
(1,’大家探听Ajax技能呢?’,’ajaxuser’,’怎么着学习Ajax手艺吗?’,1),(2,’通超过实际例学习应该准确’,’tom’,’先看看基础概念,然后多从实例中读书。’,1),(3,’多谢!’,’max’,’特别谢谢你的提出!’,1);

  1. 编写ajax发送和吸收接纳函数:
    ajax发送函数chat_send_ajax.php

<?php

 

index.php文件

<?php
   require_once(‘dbconnect.php’);
   db_connect();
   $msg =
iconv(“UTF-8″,”GB2312”,$_GET[“msg”]);
   $dt = date(“Y-m-d H:i:s”);
   $user = iconv(“UTF-8″,”GB2312”,$_GET[“name”]);
   $sql=”INSERT INTO chat(USERNAME,CHATDATE,MSG) ” .
     “values(” . quote($user) . “,” . quote($dt) . “,” . quote($msg) .
“);”;
     echo $sql;
   $result = mysql_query($sql);
   if(!$result)
   {
    throw new Exception(‘Query failed: ‘ . mysql_error());
    exit();
   }
?>

function db_connect()
{

//————–用户名检查测量检验———————//
function ck_user(result)
{
  if ( result == “true” )
  { 
    document.getelementbyid(‘username’).classname = “framedivwarn”;
 showinfo(“username_notice”,msg_un_registered);
    change_submit(“true”);//禁止使用提交按键
  }
  else
  {
    document.getelementbyid(‘username’).classname = “framedivpass”;
 showinfo(“username_notice”,msg_can_rg);
    change_submit(“false”);//可用提交按键
  }
}

function checks(t){
    szmsg=”[#%&'”,;:=!^@]”;
     //alertstr=””;
    for(i=1;i<szmsg.length+1;i++){
     if(t.indexof(szmsg.substring(i-1,i))>-1){
      //alertstr=”请勿富含不合法字符如[#_%&'”,;:=!^]”;
      return false;
     }
    }
    return true;
   }
//———–email检测——————————–//
function checkemail(email)
{
 if (chekemail(email.value)==false)

  {
    email.classname = “framedivwarn”;
bf88必发唯一官网, showinfo(“email_notice”,msg_email_format);
 change_submit(“true”); 
 }
 
else
   {
   showinfo(“email_notice”,info_right);
   email.classname = “framedivpass”;
   change_submit(“false”);
   }
}

function chekemail(temail) { 
 var pattern =
/^[w]{1}[w.-_]*@[w]{1}[w-_.]*.[w]{2,4}$/i; 
 if(pattern.test(temail)) { 
  return true; 
 } 
 else { 
  return false; 
 } 
}
//——————–密码检查实验—————————–//
function check_password( password )
{
    if ( password.value.length < 6 )
    {
  showinfo(“password_notice”,password_shorter_s);
  password.classname = “framedivwarn”;
  change_submit(“true”);//禁止使用提交开关
    }
 else if(password.value.length > 30){
  showinfo(“password_notice”,password_shorter_m);
  password.classname = “framedivwarn”;
  change_submit(“true”);//禁止使用提交开关
  }
    else
    {
  showinfo(“password_notice”,info_right);
  password.classname = “framedivpass”;
  change_submit(“false”);//允许提交按键
【bf88必发唯一官网】php无刷新回贴和注册查证实例,ajax验证用户名完成代码。    }
}

function check_conform_password( conform_password )
{
    password = document.getelementbyid(‘password’).value;
   
    if ( conform_password.value.length < 6 )
    {
  showinfo(“conform_password_notice”,password_shorter_s);
  conform_password.classname = “framedivwarn”;
  change_submit(“true”);//禁止使用提交按
        return false;
    }
    if ( conform_password.value!= password)
    {
  showinfo(“conform_password_notice”,confirm_password_invalid);
  conform_password.classname = “framedivwarn”;
  change_submit(“true”);//禁止使用提交按
    }
    else
    {  
     conform_password.classname = “framedivpass”;
  showinfo(“conform_password_notice”,info_right);
  change_submit(“false”);//允许提交开关
    }
}
//* *——————–检查测验密码强度—————————–*
*//

function checkintensity(pwd)
{
  var mcolor = “#fff”,lcolor = “#fff”,hcolor = “#fff”;
  var m=0;

  var modes = 0;
  for (i=0; i<pwd.length; i++)
  {
    var chartype = 0;
    var t = pwd.charcodeat(i);
    if (t>=48 && t <=57)
    {
      chartype = 1;
    }
    else if (t>=65 && t <=90)
    {
      chartype = 2;
    }
    else if (t>=97 && t <=122)
      chartype = 4;
    else
      chartype = 4;
    modes |= chartype;
  }

  for (i=0;i<4;i++)
  {
    if (modes & 1) m++;
      modes>>>=1;
  }

  if (pwd.length<=4)
  {
    m = 1;
  }

  switch(m)
  {
    case 1 :
      lcolor = “2px solid red”;
      mcolor = hcolor = “2px solid #dadada”;
    break;
    case 2 :
      mcolor = “2px solid #f90”;
      lcolor = hcolor = “2px solid #dadada”;
    break;
    case 3 :
      hcolor = “2px solid #3c0”;
      lcolor = mcolor = “2px solid #dadada”;
    break;
    case 4 :
      hcolor = “2px solid #3c0”;
      lcolor = mcolor = “2px solid #dadada”;
    break;
    default :
      hcolor = mcolor = lcolor = “”;
    break;
  }
  document.getelementbyid(“pwd_lower”).style.borderbottom  =
lcolor;
  document.getelementbyid(“pwd_middle”).style.borderbottom =
mcolor;
  document.getelementbyid(“pwd_high”).style.borderbottom   = hcolor;

}
//————–注册协议复选框状态检查实验———————//
function check_agreement(){
  if (document.formuser.agreement.checked==false)
  {
  showinfo(“agreement_notice”,agreement);
     change_submit(“true”);//允许提交
}
  else
  {
 showinfo(“agreement_notice”,info_right);
 change_submit(“false”);//允许提交按
 }
}

//————-处理登记程序—————————–//
function register() {
if(document.formuser.username.value==””)
 {
 showclass(“username”,”framedivwarn”);
 showinfo(“username_notice”,msg_un_blank);
   document.formuser.username.focus();
   return false;
  }
 else if(document.formuser.email.value==””)
 {
   showclass(“email”,”framedivwarn”);
   showinfo(“email_notice”,msg_email_blank);
   document.formuser.email.focus();
   return false;
  } 

 else if(document.formuser.password.value==””)
 {
 showclass(“password”,”framedivwarn”);
 showinfo(“password_notice”,password_empty);
      document.formuser.password.focus();
   return false;
  }
 else if(document.formuser.confirm_password.value==””)
 {
 showclass(“confirm_password”,”framedivwarn”);
 showinfo(“conform_password_notice”,confirm_password_invalid);
      document.formuser.password.focus();
   return false;
  }
 else if(document.formuser.agreement.checked==false)
 {
 //showclass(“agreement”,”framedivwarn”);
 showinfo(“agreement_notice”,agreement);
      document.formuser.agreement.focus();
   return false;
  }
}

//———— 开关状态设置—————————–//
function change_submit(zt)
{
     if (zt == “true”)
     {
   document.forms[‘formuser’].elements[‘submit1’].disabled =
‘disabled’;
     }
   else
     {
   document.forms[‘formuser’].elements[‘submit1’].disabled = ”;
     }
}
//——公用程序————————————//
 function showinfo(target,infos){
    document.getelementbyid(target).innerhtml = infos;
 }
 function showclass(target,infos){
    document.getelementbyid(target).classname = infos;
 } 
var process_request = “<img src=’loading.gif’ width=’16’
height=’16’ border=’0′ align=’absmiddle’>正在数据管理中…”;
var username_empty = “<span style=’color:#ff0000′>  ×
用户名不可能为空!</span>”;
var username_shorter = “<span style=’color:#ff0000′> ×
用户名长度不能够简单 3 个字符。</span>”;
var username_invalid = “- 用户名只可以是由字母数字以及下划线组成。”;
var password_empty = “<span style=’color:#ff0000′> ×
登陆密码无法为空。</span>”;
var password_shorter_s = “<span style=’color:#ff0000′> ×
登入密码不可能简单 6 个字符。</span>”;
var password_shorter_m = “<span style=’color:#ff0000′> ×
登陆密码不能够多于 30 个字符。</span>”;
var confirm_password_invalid = “<span style=’color:#ff0000′>
× 三遍输入密码不等同!</span>”;
var email_empty = “<span style=’color:#ff0000′> × email
为空</span>”;
var email_invalid = “- email 不是官方的地方”;
var agreement = “<span style=’color:#ff0000′> ×
您未有经受协议</span>”;
var msn_invalid = “- msn地址不是三个管用的邮件地址”;
var qq_invalid = “- qq号码不是三个灵光的号子”;
var home_phone_invalid = “- 家庭电话不是八个管用号码”;
var office_phone_invalid = “- 办公电话不是三个灵光号码”;
var mobile_phone_invalid = “- 手提式有线电电话机号码不是三个卓有功用号码”;
var msg_un_blank = “<span style=’color:#ff0000′> ×
用户名无法为空!</span>”;
var msg_un_length = “<span style=’color:#ff0000′> ×
用户名最长不得超越拾八个字符</span>”;
var msg_un_format = “<span style=’color:#ff0000′> ×
用户名含有不法字符!</span>”;
var msg_un_registered = “<span style=’color:#ff0000′> ×
用户名早就存在,请重新输入!</span>”;
var msg_can_rg = “<span style=’color:#006600′> √
能够挂号!</span>”;
var msg_email_blank = “<span style=’color:#ff0000′> ×
邮件地址无法为空!</span>”;
var msg_email_registered = ” × 邮箱已存在,请重新输入!”;
var msg_email_format = “<span style=’color:#ff0000′> ×
邮件地址非法!</span>”;
var username_exist = “用户名 %s 已经存在”;
var info_can=”<span style=’color:#006600′> √
能够注册!</span>”;
var info_right=”<span style=’color:#006600′> √
填写正确!</span>”;
</script>

 代码如下

ajax接收函数chat_recv_ajax.php

  date_default_timezone_set(“Asia/Shanghai”);

 

<html>
<head>
<meta http-equiv=”Content-type” content=”text/html;
charset=utf-8″>
<link href=”bbs.css”
type=”text/css” rel=”stylesheet”>
<title>无刷新展现回帖</title>
<script src=”bbs.js”></script>
</head>

<?php
header(“Content-Type:text/html;charset=gb2312”);
header(“Expires: Thu, 01 Jan 1970 00:00:01 GMT”);
  header(“Cache-Control: no-cache, must-revalidate”);
  header(“Pragma: no-cache”);
   require_once(‘dbconnect.php’);
   db_connect();
   $sql = “SELECT *, date_format(chatdate,’%Y年%m月%d日 %r’) as cdt
from chat order by ID desc limit 200”;
   $sql = “SELECT * FROM (” . $sql . “) as ch order by ID”;
   $result = mysql_query($sql) or die(‘Query failed: ‘ .
mysql_error());
   // Update Row Information
   $msg=”<table border=’0′ style=’font-size: 10pt; color: white;
font-family: verdana, arial;’>”;
   while ($line = mysql_fetch_array($result, MYSQL_ASSOC))
   {
      $msg = $msg . “<tr><td>” . $line[“cdt”] . ”
</td>” .
        “<td>” . $line[“username”] . “: </td>” .
        “<td>” . $line[“msg”] . “</td></tr>”;
   }
   $msg=$msg . “</table>”;
   echo $msg;
?>

  $link = mysql_connect(“xxx.xxx.xxx.xxx”, “databasename”,
“password”)
            or die(‘不恐怕连接: ‘ . mysql_error());
 
mysql_select_db(“databasename”)
or die(‘未有您找到钦命数量库’);
  return true;
}

 

<body>
<h1>无刷新突显回帖</h1>
<div id=”thread”>
<?
 $conn = @mysql_connect(“localhost”,”root”,”123″) or die
(“MySql连接错误”);
 mysql_select_db(“xin”,$conn);
 mysql_query(“set names ‘utf8′”);
 $sql = “select * from bbs_post where threadid = 1 order by id asc”;
 $query = mysql_query($sql);
 while($row = mysql_fetch_array($query)){
?>
   <div class=”post” id=”post<?=$row[id]?>”>
                <div class=”post_title”><?=$row[title]?>
[<?=$row[username]?>]</div>
                <div
class=”post_content”><pre><?=$row[content]?></pre></div>
         </div>
<?
 }
?>
</div>

4.聊天室页面:

 

 

<table class=”reply”>
<tr>
    <td colspan=”2″ class=”title”>回帖<input type=”hidden”
name=”threadid” id=”threadid” value=”1″></td>
</tr>
<tr>
    <td>姓名:</td>
    <td><input type=”text” name=”username”
id=”username”></td>
</tr>
<tr>
    <td>标题:</td>
    <td><input type=”text” name=”post_title”
id=”post_title”></td>
</tr>
<tr>
    <td>内容:</td>
    <td><textarea
name=”post_content”
id=”post_content”></textarea></td>
</tr>
<tr>
    <td colspan=”2″><input type=”button” onclick=”submitPost()”
value=”提交”></td>
</tr>
</table>

chat.html

function quote($strText)
{
    $Mstr = addslashes($strText);
    return “‘” . $Mstr . “‘”;
}

</head>

<body>
<div id=”reg”>
<h1>ajax用户注册验证</h1>
<form name=”formuser” onsubmit=”return register();”
action=user.php
method=post>
  <br>
  <table width=”100%” align=center border=0>
    <tbody>
      <tr>
        <td align=right
width=”15%”><strong>用户名:</strong></td>
        <td width=”57%”><input id=”username”
onblur=”chkusername(this)”
      name=”username”>
            <span id=”username_notice”
>*</span></td>
      </tr>
      <tr>
        <td
align=right><strong>email:</strong></td>
        <td><input id=”email” onblur=”checkemail(this)”
name=”email”>
            <span id=email_notice >*</span></td>
      </tr>
      <tr>
        <td
align=right><strong>密码:</strong></td>
        <td><input id=”password”
onblur=”check_password(this)”
      onkeyup=”checkintensity(this.value)” type=”password”
name=”password”>
            <span
      id=password_notice >*</span></td>
      </tr>
      <tr>
        <td
align=right><strong>密码强度:</strong></td>
        <td><table cellspacing=0 cellpadding=1 width=145
border=0>
          <tbody>
            <tr align=middle>
              <td id=pwd_lower width=”33%”>弱</td>
              <td id=pwd_middle width=”33%”>中</td>
              <td id=pwd_high width=”33%”>强</td>
            </tr>
          </tbody>
        </table></td>
      </tr>
      <tr>
        <td
align=right><strong>确认密码:</strong></td>
        <td><input id=”conform_password”
onblur=”check_conform_password(this)”
      type=”password” name=”confirm_password”>
            <span id=conform_password_notice
>*</span></td>
      </tr>
      <tr>
        <td
align=right><strong>msn:</strong></td>
        <td><input name=other[msn]>        </td>
      </tr>
      <tr>
        <td
align=right><strong>qq:</strong></td>
        <td><input name=other[qq]>        </td>
      </tr>
      <tr>
        <td
align=right><strong>办公电话:</strong></td>
        <td><input name=other[office_phone]>       
</td>
      </tr>
      <tr>
        <td
align=right><strong>家庭电话:</strong></td>
        <td><input name=other[home_phone]>       
</td>
      </tr>
      <tr>
        <td
align=right><strong>手机:</strong></td>
        <td><input name=other[mobile_phone]>       
</td>
      </tr>
      <tr>
        <td> </td>
        <td><label>
          <input type=”checkbox” checked value=”1″ name=”agreement”
onblur=”check_agreement(this)”>
          <b>作者已看过并接受《<a
href=”#”>用户协议</a>》<span id=agreement_notice
>*</span></b></label></td>
      </tr>
      <tr>
        <td  ><input type=hidden value=act_register
name=act></td>
        <td  ><input type=submit
value=确认挂号    name=”submit1″ class=”anniu”
disabled></td>
      </tr>
      <tr>
        <td colspan=2> </td>
      </tr>
    </tbody>
  </table>
</form>
</div>
</body>
</html>

上面代码保存成user_ck.php文件

<?php
function
mysql教程_open()
{
$mysql_servername=”localhost”; //服务器名称
$mysql_username=”root”;
$mysql_password=””;
$mysql_dbname=”inso”;

$conn=mysql_connect($mysql_servername ,$mysql_username
,$mysql_password);
      mysql_query(“set names utf8”); //内定字符集为utf8
      mysql_select_db($mysql_dbname , $conn);
   return $conn;
}
 //设置页面编码
header(“content-type:text/html;charset=utf-8”);
$username=trim($_get[“username”]);
$conn=mysql_open();
$sql=”select * from admin where username=’$username'”;
$query=mysql_query($sql);
$rst=mysql_fetch_object($query);
mysql_close($conn);
if ($rst==false)
{
echo ‘false’;
}
else
{
echo ‘true’;
}
?>

</body>
</html>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“;
<html xmlns=”” >
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=gb2312″
/>
  <title>聊天页面</title>
<script
type=”text/javascript”>
var t = setInterval(function(){get_chat_msg()},5000);
//
// General Ajax Call
//
var oxmlHttp;
var oxmlHttpSend;
function get_chat_msg()
{
  if(typeof XMLHttpRequest !=
“undefined”)
  {
    oxmlHttp = new XMLHttpRequest();
  }
  else if (window.ActiveXObject)
  {
    oxmlHttp = new ActiveXObject(“Microsoft.XMLHttp”);
  }
  if(oxmlHttp == null)
  {
    alert(“浏览器不帮助XML Http Request!”);
    return;
  }
  oxmlHttp.onreadystatechange = get_chat_msg_result;
  oxmlHttp.open(“GET”,encodeURI(“chat_recv_ajax.php”),true);
  oxmlHttp.send(null);
}
function get_chat_msg_result()
{
  if(oxmlHttp.readyState==4 || oxmlHttp.readyState==”complete”)
  {
    if (document.getElementById(“DIV_CHAT”) != null)
    {
      document.getElementById(“DIV_CHAT”).innerHTML =
oxmlHttp.responseText;
      oxmlHttp = null;
    }
    var scrollDiv = document.getElementById(“DIV_CHAT”);
    scrollDiv.scrollTop = scrollDiv.scrollHeight;
  }
}
function set_chat_msg()
{
  if(typeof XMLHttpRequest != “undefined”)
  {
    oxmlHttpSend = new XMLHttpRequest();
  }
  else if (window.ActiveXObject)
  {
    oxmlHttpSend = new ActiveXObject(“Microsoft.XMLHttp”);
  }
  if(oxmlHttpSend == null)
  {
    alert(“浏览器不帮助XML Http Request!”);
    return;
  }
  var url = “chat_send_ajax.php”;
  var strname=”noname”;
  var strmsg=””;
  if (document.getElementById(“txtname”) != null)
  {
    strname = document.getElementById(“txtname”).value;
    document.getElementById(“txtname”).readOnly=true;
  }
  if (document.getElementById(“txtmsg”) != null)
  {
    strmsg = document.getElementById(“txtmsg”).value;
    document.getElementById(“txtmsg”).value = “”;
  }
  url += “?name=” + strname + “&msg=” + strmsg;
  oxmlHttpSend.open(“GET”,encodeURI(url),true);
  oxmlHttpSend.send(null);
}
function clickBtn(e)
 {
  if(window.event.keyCode==13)
  {
  var id=e.id;
  switch(id)
  {
   case “txtmsg”:
   document.getElementById(“Submit2”).click();
   window.event.returnValue=false;
   break;
   }
  }
}
function fRandomBy(under, over){
switch(arguments.length){
case 1: return parseInt(Math.random()*under+1);
case 2: return parseInt(Math.random()*(over-under+1) + under);
default: return 0;
}
}
function SetTxtName(){
var i=fRandomBy(10);
if(i==0)document.getElementById(‘txtname’).value=’无敌刑天’;
if(i==1)document.getElementById(‘txtname’).value=’令狐冲’;
if(i==2)document.getElementById(‘txtname’).value=’南门吹雪’;
if(i==3)document.getElementById(‘txtname’).value=’超级Mary’;
if(i==4)document.getElementById(‘txtname’).value=’奥巴马’;
if(i==5)document.getElementById(‘txtname’).value=’恐怖分子’;
if(i==6)document.getElementById(‘txtname’).value=’聊斋奇女生’;
if(i==7)document.getElementById(‘txtname’).value=’天朝?潘?;
if(i==8)document.getElementById(‘txtname’).value=’中500万了’;
if(i==9)document.getElementById(‘txtname’).value=’神级奇葩’;
if(i==10)document.getElementById(‘txtname’).value=’爱你不是两三日’;
}
</script>
</head>
<body onload=”SetTxtName();”>
  <div style=”border-right: black thin solid; border-top: black thin
solid;
    border-left: black thin solid; border-bottom: black thin solid;
    background:#fff url(”)
repeat-x left top;
    height: 450px;width: 500px; “>
    <table style=”width:100%; height:100%”>
      <tr>
        <td colspan=”2″ style=”font-weight: bold; font-size: 16pt;
color: white; font-family: verdana, arial;
          text-align: center”>
          聊天窗口–全球最大QQ聊天交友网址</td>
      </tr>
      <tr>
        <td colspan=”2″ style=”font-weight: bold; font-size: 16pt;
color: white; font-family: verdana, arial;
          text-align: left”>
          <table style=”font-size: 12pt; color: white; font-family:
Verdana, Arial;border: white thin solid; “>
            <tr>
              <td style=”width: 100px”>
                名字:</td>
              <td style=”width: 100px”><input id=”txtname”
style=”width: 150px” type=”text” name=”name” maxlength=”15″ value=”匿名”
/></td>
            </tr>
          </table>
        </td>
      </tr>
      <tr>
        <td style=”vertical-align: middle;” valign=”middle”
colspan=”2″>
          <div style=”width: 480px; height: 300px; border-right:
white thin solid; border-top: white thin solid; font-size: 10pt;
border-left: white thin solid; border-bottom: white thin solid;
font-family: verdana, arial; overflow:scroll; text-align: left;”
id=”DIV_CHAT”>
          </div>
        </td>
      </tr>
      <tr>
        <td style=”width: 310px”>
          <input id=”txtmsg” style=”width: 350px” type=”text”
name=”msg” onkeydown=”return clickBtn(this)”/></td>
        <td style=”width: 85px”>
          <input id=”Submit2″ style=”font-family: verdana, arial”
type=”button” value=”发送” onclick=”set_chat_msg()”/></td>
      </tr>
      <tr>
        <td colspan=”1″ style=”font-family: verdana, arial;
text-align: center; width: 350px;”>
          </td>
        <td colspan=”1″ style=”width: 85px; font-family: verdana,
arial; text-align: center”>
        </td>
      </tr>
    </table>
  </div>
</body>
</html>
功效图如下:

function isdate($d)
{
   $ret = true;
   try
   {
       $x = date(“d”,$d);
   }
   catch (Exception $e)
   {
       $ret = false;
   }
   echo $x;
   return $ret;
}

bbspost.php文件

 
?>

 

  1. 编辑ajax发送和接受函数:

 代码如下

ajax发送函数chat_send_ajax.php

<?php
$title = $_POST[“title”]; //获取title
$content = $_POST[“content”]; //获取content
$username = $_POST[“username”]; //获取username
$threadId = $_POST[“threadid”]; //获取threadid

<?php
     require_once(‘dbconnect.php’);

$conn = @ mysql_connect(“localhost”, “root”, “123”) or
die(“MySql连接错误”);
mysql_select_db(“xin”, $conn);
mysql_query(“set names ‘utf8′”);

     db_connect();

$sql=”insert into bbs_post (title,content,username,threadid) ” .
    “values (‘$title’,’$content’,’$username’,’$threadId’)”;
   echo $sql;
   mysql_query($sql);
  //传回最终叁遍利用 INSERT 指令的 ID
 $responseId=mysql_insert_id();
 echo $responseId;
?>

     $msg =
iconv(“UTF-8″,”GB2312”,$_GET[“msg”]);
     $dt = date(“Y-m-d H:i:s”);
     $user = iconv(“UTF-8″,”GB2312”,$_GET[“name”]);

 

     $sql=”INSERT INTO chat(USERNAME,CHATDATE,MSG) ” .
          “values(” . quote($user) . “,” . quote($dt) . “,” .
quote($msg) . “);”;

bbs.js文件,里面富含了大批量ajax文件啊

          echo $sql;

 代码如下

     $result = mysql_query($sql);
     if(!$result)
     {
        throw new Exception(‘Query failed: ‘ . mysql_error());
        exit();
     }

//先创制三个空的bbs.js文件,并修改其属性为utf-8,技术保留汉语。
var xmlHttp;                       
//用于保存XMLHttpRequest对象的全局变量
var username;                       //用于保存姓名
var title;                          //用于保存标题
var content;                        //用于保存内容
var threadid;                       //用于保存主题编号

?>

//用于创制XMLHttpRequest对象
function createXmlHttp() {
    //依照window.XMLHttpRequest对象是还是不是留存使用差异的创建立模型式
    if (window.XMLHttpRequest) {
       xmlHttp = new XMLHttpRequest();                 
//FireFox、Opera等浏览器援救的创建方式
    } else {
       xmlHttp = new
ActiveXObject(“Microsoft.XMLHTTP”);//IE浏览器支撑的创造情势
    }
}

 

//提交回帖到服务器
function submitPost() {
    //获取帖子中姓名、标题、内容、焦点编号四有的音讯
    username = document.getElementById(“username”).value;
    title = document.getElementById(“post_title”).value;
    content = document.getElementById(“post_content”).value;
    threadid = document.getElementById(“threadid”).value;
 alert(username+””+title+””+content+””+threadid);
    if (checkForm()) {
        createXmlHttp();                                   
//创建XMLHttpRequest对象
        xmlHttp.onreadystatechange = submitPostCallBack;   
//设置回调函数
        xmlHttp.open(“POST”, “bbs_post.php”, true);        
//发送POST请求
        //设置POST央求体类型
       
xmlHttp.setRequestHeader(“Content-type”,”application/x-www-form-urlencoded”);
        xmlHttp.send(“username=” + encodeURI(username) +
                     “&title=” + encodeURI(title) +
                     “&content=” + encodeURI(content) +
                     “&threadid=” + threadid);             
//发送包涵三个参数的央浼体
    }
}

ajax接收函数chat_recv_ajax.php

//检查表单是或不是内容已填写完成
function checkForm() {
    if (username == “”) {
        alert(“请填入姓名”);
        return false;
    } else if (title == “”) {
        alert(“请填入标题”);
        return false;
    } else if (content == “”) {
        alert(“请填写内容”);
        return false;
    }
    return true;
}

<?php
header(“Content-Type:text/html;charset=gb2312”);
header(“Expires: Thu, 01 Jan 1970 00:00:01 GMT”);
   header(“Cache-Control: no-cache, must-revalidate”);
   header(“Pragma: no-cache”);
     require_once(‘dbconnect.php’);

//获取查询选取的回调函数
function submitPostCallBack() {
    if (xmlHttp.readyState == 4) {
     alert(xmlHttp.responseText);
        createNewPost(xmlHttp.responseText);
    }
}

     db_connect();
    
     $sql = “SELECT *, date_format(chatdate,’%Y年%m月%d日 %r’) as cdt
from chat order by ID desc limit 200”;
     $sql = “SELECT * FROM (” . $sql . “) as ch order by ID”;
     $result = mysql_query($sql) or die(‘Query failed: ‘ .
mysql_error());
    
     // Update Row Information
     $msg=”<table border=’0′ style=’font-size: 10pt; color: white;
font-family: verdana, arial;’>”;
     while ($line = mysql_fetch_array($result, MYSQL_ASSOC))
     {
           $msg = $msg . “<tr><td>” . $line[“cdt”] . ”
</td>” .
                “<td>” . $line[“username”] . “: </td>” .
                “<td>” . $line[“msg”] .
“</td></tr>”;
     }
     $msg=$msg . “</table>”;
    
     echo $msg;

//创设新的回帖
function createNewPost(postId) {
    //清空当前表单中各部分音信
    document.getElementById(“post_title”).value = “”;
    document.getElementById(“post_content”).value = “”;
    document.getElementById(“username”).value = “”;

?>

    var postDiv = createDiv(“post”, “”);    //成立回帖的外围div
    postDiv.id = “post” + postId;           //给新div赋id值

 

    var postTitleDiv = createDiv(“post_title”, title + ” [” + username

4.聊天室页面:

  • “]”);  //创造标题div
        var postContentDiv = createDiv(“post_content”, “<pre>” +
    content + “</pre>”);    //创立内容div
        postDiv.appendChild(postTitleDiv);                         
    //在外层div追加标题
        postDiv.appendChild(postContentDiv);                       
    //在外层div追加内容

chat.html

    document.getElementById(“thread”).appendChild(postDiv);    
//将外层div追加到核心div中
}

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“;
<html xmlns=”” >
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=gb2312″
/>
    <title>聊天页面</title>
  
<script
type=”text/javascript”>

//依据className和text创设新的div
function createDiv(className, text) {
    var newDiv = document.createElement(“div”);
    newDiv.className = className;
    newDiv.innerHTML = text;
    return newDiv;
}

var t = setInterval(function(){get_chat_msg()},5000);

css文件如下

//
// General Ajax Call
//
     
var oxmlHttp;
var oxmlHttpSend;
     
function get_chat_msg()
{

 代码如下

    if(typeof XMLHttpRequest !=
“undefined”)
    {
        oxmlHttp = new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
       oxmlHttp = new ActiveXObject(“Microsoft.XMLHttp”);
    }
    if(oxmlHttp == null)
    {
        alert(“浏览器不协助XML Http Request!”);
       return;
    }
   
    oxmlHttp.onreadystatechange = get_chat_msg_result;
    oxmlHttp.open(“GET”,encodeURI(“chat_recv_ajax.php”),true);
    oxmlHttp.send(null);
}
    
function get_chat_msg_result()
{
    if(oxmlHttp.readyState==4 || oxmlHttp.readyState==”complete”)
    {
        if (document.getElementById(“DIV_CHAT”) != null)
        {
            document.getElementById(“DIV_CHAT”).innerHTML = 
oxmlHttp.responseText;
            oxmlHttp = null;
        }
        var scrollDiv = document.getElementById(“DIV_CHAT”);
        scrollDiv.scrollTop = scrollDiv.scrollHeight;
    }
}

/* 页面基本样式 */
body, td, input, textarea {
    font-family:Arial;
    font-size:12px;
}

     
function set_chat_msg()
{

/* 大旨的体裁 */
#thread {
    border:1px solid black;
    width:300px;
    margin-bottom:10px;
}

    if(typeof XMLHttpRequest != “undefined”)
    {
        oxmlHttpSend = new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
       oxmlHttpSend = new ActiveXObject(“Microsoft.XMLHttp”);
    }
    if(oxmlHttpSend == null)
    {
       alert(“浏览器不帮衬XML Http Request!”);
       return;
    }
   
    var url = “chat_send_ajax.php”;
    var strname=”noname”;
    var strmsg=””;
    if (document.getElementById(“txtname”) != null)
    {
        strname = document.getElementById(“txtname”).value;
        document.getElementById(“txtname”).readOnly=true;
    }
    if (document.getElementById(“txtmsg”) != null)
    {
        strmsg = document.getElementById(“txtmsg”).value;
        document.getElementById(“txtmsg”).value = “”;
    }
   
    url += “?name=” + strname + “&msg=” + strmsg;
    oxmlHttpSend.open(“GET”,encodeURI(url),true);
    oxmlHttpSend.send(null);
}
function clickBtn(e)
  {
   if(window.event.keyCode==13)
   {
    var id=e.id;
    switch(id)
    {
     case “txtmsg”:
      document.getElementById(“Submit2”).click();
      window.event.returnValue=false;
      break;
     }
   }
}
function fRandomBy(under, over){
switch(arguments.length){
case 1: return parseInt(Math.random()*under+1);
case 2: return parseInt(Math.random()*(over-under+1) + under);
default: return 0;
}
}
function SetTxtName(){
var i=fRandomBy(10);
if(i==0)document.getElementById(‘txtname’).value=’无敌刑天’;
if(i==1)document.getElementById(‘txtname’).value=’令狐冲’;
if(i==2)document.getElementById(‘txtname’).value=’北门吹雪’;
if(i==3)document.getElementById(‘txtname’).value=’一级Mary’;
if(i==4)document.getElementById(‘txtname’).value=’奥巴马’;
if(i==5)document.getElementById(‘txtname’).value=’恐怖分子’;
if(i==6)document.getElementById(‘txtname’).value=’聊斋奇女人’;
if(i==7)document.getElementById(‘txtname’).value=’天朝?潘?;
if(i==8)document.getElementById(‘txtname’).value=’中500万了’;
if(i==9)document.getElementById(‘txtname’).value=’神级奇葩’;
if(i==10)document.getElementById(‘txtname’).value=’爱你不是两四日’;
}
</script>

/* 提示新闻div的体裁 */
#statusDiv {
    border:1px solid #999;
    background:#FFFFCC;
    width:100px;
    position:absolute;
    top:50%;
    left:50%;
    margin:-50px 0 0 -100px;
    width:280px;
}

</head>
<body onload=”SetTxtName();”>
    
    <div style=”border-right: black thin solid; border-top: black
thin solid;
        border-left: black thin solid; border-bottom: black thin
solid;
        background:#fff url(”)
repeat-x left top;
        height: 450px;width: 500px; “>
        <table style=”width:100%; height:100%”>
            <tr>
                <td colspan=”2″ style=”font-weight: bold; font-size:
16pt; color: white; font-family: verdana, arial;
                    text-align: center”>
                    聊天窗口–环球最大QQ聊天交友网址</td>
            </tr>
            <tr>
                <td colspan=”2″ style=”font-weight: bold; font-size:
16pt; color: white; font-family: verdana, arial;
                    text-align: left”>
                    <table style=”font-size: 12pt; color: white;
font-family: Verdana, Arial;border: white thin solid; “>
                        <tr>
                            <td style=”width: 100px”>
                                名字:</td>
                            <td style=”width: 100px”><input
id=”txtname” style=”width: 150px” type=”text” name=”name” maxlength=”15″
value=”匿名” /></td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr>
                <td style=”vertical-align: middle;” valign=”middle”
colspan=”2″>
                    <div style=”width: 480px; height: 300px;
border-right: white thin solid; border-top: white thin solid; font-size:
10pt; border-left: white thin solid; border-bottom: white thin solid;
font-family: verdana, arial; overflow:scroll; text-align: left;”
id=”DIV_CHAT”>
                    </div>
                </td>
            </tr>
            <tr>
                <td style=”width: 310px”>
                    <input id=”txtmsg” style=”width: 350px”
type=”text” name=”msg” onkeydown=”return
clickBtn(this)”/></td>
                <td style=”width: 85px”>
                    <input id=”Submit2″ style=”font-family: verdana,
arial” type=”button” value=”发送”
onclick=”set_chat_msg()”/></td>
            </tr>
            <tr>
                <td colspan=”1″ style=”font-family: verdana, arial;
text-align: center; width: 350px;”>
                    </td>
                <td colspan=”1″ style=”width: 85px; font-family:
verdana, arial; text-align: center”>
                </td>
            </tr>
        </table>
    </div>
</body>
</html>

/* 帖子的样式 */
div.post {
    border-bottom:1px solid black;
    padding:5px;
}

 

/* 帖子title的样式 */
div.post_title {
    border-bottom:1px dotted #0066CC;
    font-weight:bold;
}

效果:

/* 帖子content的样式 */
div.post_content {
    font-size:12px;
    margin:5px;
}

 

/* 回帖表格基本样式 */
table.reply {
    border-collapse:collapse;
    width:300px;
}

bf88必发唯一官网 1

/* 回帖表格单元格样式 */
table.reply td {
    border:1px solid black;
    padding:3px;
}

/* 回帖表格表头样式 */
table.reply td.title {
    background:#003366;
    color:#FFFFFF;
}

/* 表单元素样式 */
input, textarea {
    border:1px solid black;
}

/* 文字区域样式 */
textarea {
    width:200px;
    height:50px;
}

/* 预约义格式样式 */
pre {
    margin:0;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图