ajax与php web开发01.docx

上传人:b****6 文档编号:4153282 上传时间:2022-11-28 格式:DOCX 页数:10 大小:17.68KB
下载 相关 举报
ajax与php web开发01.docx_第1页
第1页 / 共10页
ajax与php web开发01.docx_第2页
第2页 / 共10页
ajax与php web开发01.docx_第3页
第3页 / 共10页
ajax与php web开发01.docx_第4页
第4页 / 共10页
ajax与php web开发01.docx_第5页
第5页 / 共10页
点击查看更多>>
下载资源
资源描述

ajax与php web开发01.docx

《ajax与php web开发01.docx》由会员分享,可在线阅读,更多相关《ajax与php web开发01.docx(10页珍藏版)》请在冰豆网上搜索。

ajax与php web开发01.docx

ajax与phpweb开发01

实现步骤——Ajax聊天

(1)连接Ajax数据库,并用下面的代码创建表chat:

CREATETABLEchat

chat_idint(11)NOTNULLauto_increment,

posted_ondatetimeNOTNULL,

user_namevarchar(255)NOTNULL,

messagetextNOTNULL,

colorchar(7)default'#000000',

PRIMARYKEY(chat_id)

);

(2)在Ajax文件夹下,创建新文件夹chat。

(3)从下载代码中将palette.png文件复制到chat文件夹下。

(4)我们将创建一个由服务器功能开始的应用。

在chat文件夹下,创建文件config.php,并把数据库配置代码加到此文件里(改变这些变量值来匹配配置)。

php

//定义数据库连接数据

define('DB_HOST','localhost');

define('DB_USER','ajaxuser');

define('DB_PASSWORD','practical');

define('DB_DATABASE','ajax');

?

>

(5)现在加入标准错误处理文件error_handler.php:

php

//设置用户错误处理方法为error_handler

set_error_handler('error_handler',E_ALL);

//错误处理函数

functionerror_handler($errNo,$errStr,$errFile,$errLine)

{

//清除所有已经生成的输出

if(ob_get_length())ob_clean();

//输出错误消息

$error_message='ERRNO:

'.$errNo.chr(10).

'TEXT:

'.$errStr.chr(10).

'LOCATION:

'.$errFile.

',line'.$errLine;

echo$error_message;

//阻止处理任何其他PHP脚本

exit;

}

?

>

(6)创建文件chat.php并加入下面代码:

php

//参考包含Chat类的文件

require_once("chat.class.php");

//取回执行的操作

$mode=$_POST['mode'];

//默认情况下最后的id是0

$id=0;

//创建一个新的聊天实例

$chat=newChat();

//如果操作是SendAndRetrieve

if($mode=='SendAndRetrieveNew')

{

//取回用来添加一个新消息的作用参数

$name=$_POST['name'];

$message=$_POST['message'];

$color=$_POST['color'];

$id=$_POST['id'];

//检查是否我们有合法的值

if($name!

=''&&$message!

=''&&$color!

='')

{

//把消息传递到数据库

$chat->postMessage($name,$message,$color);

}

}

//如果操作是DeleteAndRetrieve

elseif($mode=='DeleteAndRetrieveNew')

{

//删除所有已存在的消息

$chat->deleteMessages();

}

//如果操作是Retrieve

elseif($mode=='RetrieveNew')

{

//获取客户取回的最后消息的id

$id=$_POST['id'];

}

//清除输出

if(ob_get_length())ob_clean();

//发送Headers阻止浏览器缓存

header('Expires:

Mon,26Jul199705:

00:

00GMT');

header('Last-Modified:

'.gmdate('D,dMYH:

i:

s').'GMT');

header('Cache-Control:

no-cache,must-revalidate');

header('Pragma:

no-cache');

header('Content-Type:

text/xml');

//从服务器取回新消息

echo$chat->retrieveNewMessages($id);

?

>

(7)创建文件chat.class并加入下面代码:

php

//加载配置文件

require_once('config.php');

//加载错误处理模块

require_once('error_handler.php');

//包含服务端聊天功能的类

classChat

{

//数据库处理

private$mMysqli;

//构造函数打开数据库连接

function__construct()

{

//连接到数据库

$this->mMysqli=newmysqli(DB_HOST,DB_USER,DB_PASSWORD,

DB_DATABASE);

}

//析构函数关闭数据库连接

publicfunction__destruct()

{

$this->mMysqli->close();

}

//截去包含消息的表

publicfunctiondeleteMessages()

{

//建立增加一个新消息到服务器的SQL查询

$query='TRUNCATETABLEchat';

//执行SQL查询

$result=$this->mMysqli->query($query);

}

/*

postMessages方法向数据库中插入消息

-$name代表post消息的用户的名称

-$messsage是post的消息

-$color包含用户选取的颜色

*/

publicfunctionpostMessage($name,$message,$color)

{

//转换变量数据,以便安全地将它们添加到数据库

$name=$this->mMysqli->real_escape_string($name);

$message=$this->mMysqli->real_escape_string($message);

$color=$this->mMysqli->real_escape_string($color);

//建立增加一个新消息到服务器的SQL查询

$query='INSERTINTOchat(posted_on,user_name,message,color)'.

'VALUES(NOW(),"'.$name.'","'.$message.

'","'.$color.'")';

//执行SQL查询

$result=$this->mMysqli->query($query);

}

/*

retrieveNewMessages方法提取已经发送给服务器的新消息

-$id参数由客户端发送,而且它是客户端接收到的最后一个消息的id

通过$id从数据库获取最近的消息,并利用XML格式返回给客户端。

*/

publicfunctionretrieveNewMessages($id=0)

{

//转换变量数据

$id=$this->mMysqli->real_escape_string($id);

//组成取回新消息的SQL查询

if($id>0)

{

//取回比$id新的消息

$query=

'SELECTchat_id,user_name,message,color,'.

'DATE_FORMAT(posted_on,"%Y-%m-%d%H:

%i:

%s")'.

'ASposted_on'.

'FROMchatWHEREchat_id>'.$id.

'ORDERBYchat_idASC';

}

else

{

//第一次加载仅仅从服务器取回最后50个消息

$query=

'SELECTchat_id,user_name,message,color,posted_onFROM'.

'(SELECTchat_id,user_name,message,color,'.

'DATE_FORMAT(posted_on,"%Y-%m-%d%H:

%i:

%s")ASposted_on'.

'FROMchat'.

'ORDERBYchat_idDESC'.

'LIMIT50)ASLast50'.

'ORDERBYchat_idASC';

}

//执行查询

$result=$this->mMysqli->query($query);

//建立XML响应

$response='

xmlversion="1.0"encoding="UTF-8"standalone="yes"?

>';

$response.='';

//输出清除标志

$response.=$this->isDatabaseCleared($id);

//检查看我们是否有任何结果

if($result->num_rows)

{

//在所有取来的消息间循环来建立结果消息

while($row=$result->fetch_array(MYSQLI_ASSOC))

{

$id=$row['chat_id'];

$color=$row['color'];

$userName=$row['user_name'];

$time=$row['posted_on'];

$message=$row['message'];

$response.=''.$id.''.

''.$color.''.

''.

''.$userName.''.

''.$message.'';

}

//尽快关闭数据库连接

$result->close();

}

//完成XML响应并返回它

$response=$response.'';

return$response;

}

/*

isDatabaseCleared方法查看自从上次访问服务器后数据库是否被清空。

参数包含最后从客户端接收的消息的id。

*/

privatefunctionisDatabaseCleared($id)

{

if($id>0)

{

//通过检查其id号比客户的最后id号小的行数,我们检查看是否一个截去操作同时//被执行

$check_clear='SELECTcount(*)oldFROMchatwherechat_id<='.$id;

$result=$this->mMysqli->query($check_clear);

$row=$result->fetch_array(MYSQLI_ASSOC);

//如果一个截去操作发生了,白色书写板需要被重新设置

if($row['old']==0)

return'true';

}

return'false';

}

}

?

>

(8)创建文件get_color.php并加入下面代码:

php

//图片文件的名字

$imgfile='palette.png';

//加载图片文件

$img=imagecreatefrompng($imgfile);

//获得用户单击点的坐标

$offsetx=$_GET['offsetx'];

$offsety=$_GET['offsety'];

//获取单击的颜色

$rgb=ImageColorAt($img,$offsetx,$offsety);

$r=($rgb>>16)&0xFF;

$g=($rgb>>8)&0xFF;

$b=$rgb&0xFF;

//返回颜色代码

printf('#%02s%02s%02s',dechex($r),dechex($g),dechex($b));

?

>

(9)现在开始处理客户端了。

先创建文件chat.css并加入下面代码:

body

{

font-family:

Tahoma,Helvetica,sans-serif;

margin:

1px;

font-size:

12px;

text-align:

left

}

#content

{

border:

DarkGreen1pxsolid;

margin-bottom:

10px

}

input

{

border:

#9991pxsolid;

font-size:

10px

}

#scroll

{

position:

relative;

width:

340px;

height:

270px;

overflow:

auto

}

.item

{

margin-bottom:

6px

}

#colorpicker

{

text-align:

center

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 小学教育 > 数学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1