基于SAE的Ajax留言板

Sina App Engine(简称SAE)是新浪研发中心推出的国内首个公有云计算平台,支持PHP,MySQL,Memcached,Mail,TaskQueue,RDC(关系型数据库集群)等服务。

本文构建的ajax留言板基于该平台。

 

用到的技术:

·PHP

·MySql

·Ajax

·SAE API

 

本应用包含一个数据库表message,两个php页面,以下为详细介绍:

数据库表
message(id,email,content)
包括2个php页面:message.php(留言页面)和message_ajax.php(处理异步请求)

message.php

<?php

#[Select message from DB]
$mysql = new SaeMysql();
$sql = "SELECT * FROM `message` order by id desc LIMIT 10";
$data = $mysql->getData( $sql );
#print_r($data);

if( $mysql->errno() != 0 )
{
die( "Error:" . $mysql->errmsg() );
}

$mysql->closeDb();


?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Leave Message</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script language="javascript" type="text/javascript">
var xmlHttp;
function createXMLHttpRequest(){
if(window.ActiveXObject)
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)
{
xmlHttp = new XMLHttpRequest();
}else{
alert("浏览器不支持XMLHttpRequest对象");
}
}

function createQueryString(){
var email = document.getElementById("email").value;
var content = document.getElementById("content").value;
var queryString = "email="+ email + "&content="+ content ;
//alert(queryString);
return queryString;
}

function doRequestUsingPOST(){
createXMLHttpRequest();

var url = "message_ajax.php?timeStamp=" + new Date().getTime();
var queryString = createQueryString();
xmlHttp.open("POST",url,true);
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
xmlHttp.send(queryString);
}

function handleStateChange(){
if(xmlHttp.readyState == 4){
if(xmlHttp.status == 200){
parseResult();
//alert("OK");
}
}
}

function parseResult(){

var responseDiv = document.getElementById("newmessage");
if(responseDiv.hasChildNodes()){
responseDiv.removeChild(responseDiv.childNodes[0]);
}
var responseText = xmlHttp.responseText;//document.createTextNode(xmlHttp.responseText);
responseDiv.innerHTML = responseText;
//responseDiv.appendChild(responseText);
}


</script>
</head>

<body>
<div style="width:800px;margin:0 auto;">
<form action="#" >
<fieldset>
<legend>Leave Message Here</legend>
<table>
<tr>
<td>Email:</td>
<td><input name="email" id="email" type="email" /></td>
</tr>
<tr>
<td>Content:</td>
<td><textarea name="content" id="content" cols="80" rows="6" ></textarea></td>
</tr>
<tr>
<td></td>
<td><input name="button1" type="button" value="Post" onclick="doRequestUsingPOST()" /></td>
</tr>
</table>
</fieldset>
</form>
</div>
<div style="width:800px;margin:0 auto;">
<hr style="width:800px;margin-bottom:15px;"/>
<h2>Here are the messages:</h2>
</div>
<div id="newmessage" style="width:800px;margin:0 auto;">

<table style="border:1px gray dash;">
<?php foreach($data as $message){ ?>
<tr>
<th style="background-color:gray;">Email:</th>
<td><?php echo $message['email']; ?></td>
</tr>
<tr>
<th style="background-color:gray;">Content:</th>
<td><?php echo $message['content']; ?></td>
</tr>
<tr><td colspan="2"></td></tr>
<?php } ?>
</table>
</div>
</body>
</html>


message_ajax.php

<?php
$mysql = new SaeMysql();
$email = strip_tags($_POST['email']);
$content = strip_tags($_POST['content']);
$sql1 = "insert into message values(NULL, '" . $mysql->escape($email) . "' ,'".$mysql->escape($content)."')";
$mysql->runSql( $sql1 );


$sql = "SELECT * FROM `message` order by id desc LIMIT 10";
$data = $mysql->getData($sql);
$responseText = "<table>";

foreach($data as $message){
$onemessage = "
<tr>
<th style='background-color:gray;'>Email:</th>
<td>".$message['email']."</td>
</tr>
<tr>
<th style='background-color:gray;'>Content:</th>
<td>".$message['content']."</td>
</tr>";
$responseText = $responseText.$onemessage;
}
$responseText = $responseText."</table>";
echo $responseText;


?>


演示地址:http://gofast.sinaapp.com/message.php

 

posted on 2011-12-14 23:17  IT技术畅销书  阅读(1229)  评论(0编辑  收藏  举报

导航