Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2561387
  • 博文数量: 245
  • 博客积分: 4125
  • 博客等级: 上校
  • 技术积分: 3113
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-25 23:56
文章分类

全部博文(245)

文章存档

2015年(2)

2014年(26)

2013年(41)

2012年(40)

2011年(134)

2010年(2)

分类: 系统运维

2011-10-08 16:38:38


http://blog.chinaunix.net/space.php?uid=21209537&do=blog&id=2722878

数据库表
message(id,email,content)

包括2个php页面:message.php(留言页面)和message_ajax.php(处理异步请求)

message.php

  1. <?php
  2.     
  3.     #[Select message from DB]
  4.     $mysql = new SaeMysql();
  5.     $sql = "SELECT * FROM `message` order by id desc LIMIT 10";
  6.     $data = $mysql->getData( $sql );
  7.     #print_r($data);
  8.     
  9.     if( $mysql->errno() != 0 )
  10.     {
  11.         die( "Error:" . $mysql->errmsg() );
  12.     }
  13.      
  14.     $mysql->closeDb();
  15.     

  16. ?>
  17. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  18. <html>
  19.     <head>
  20.         <title>Leave Message</title>
  21.         <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  22. <script language="javascript" type="text/javascript">
  23.   var xmlHttp;
  24.   function createXMLHttpRequest(){
  25.     if(window.ActiveXObject)
  26.       {
  27.          xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  28.       }
  29.       else if(window.XMLHttpRequest)
  30.       {
  31.         xmlHttp = new XMLHttpRequest();
  32.       }else{
  33.         alert("浏览器不支持XMLHttpRequest对象");
  34.       }
  35.   }
  36.   
  37.   function createQueryString(){
  38.     var email = document.getElementById("email").value;
  39.     var content = document.getElementById("content").value;
  40.     var queryString = "email="+ email + "&content="+ content ;
  41.     //alert(queryString);
  42.     return queryString;
  43.   }
  44.   
  45.   function doRequestUsingPOST(){
  46.     createXMLHttpRequest();
  47.     
  48.     var url = "message_ajax.php?timeStamp=" + new Date().getTime();
  49.     var queryString = createQueryString();
  50.     xmlHttp.open("POST",url,true);
  51.     xmlHttp.onreadystatechange = handleStateChange;
  52.     xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
  53.     xmlHttp.send(queryString);
  54.   }
  55.  
  56.   function handleStateChange(){
  57.      if(xmlHttp.readyState == 4){
  58.         if(xmlHttp.status == 200){
  59.            parseResult();
  60.            //alert("OK");
  61.         }
  62.      }
  63.   }
  64.   
  65.   function parseResult(){
  66.       
  67.     var responseDiv = document.getElementById("newmessage");
  68.     if(responseDiv.hasChildNodes()){
  69.       responseDiv.removeChild(responseDiv.childNodes[0]);
  70.     }
  71.     var responseText = xmlHttp.responseText;//document.createTextNode(xmlHttp.responseText);
  72.     responseDiv.innerHTML = responseText;
  73.     //responseDiv.appendChild(responseText);
  74.   }
  75.   

  76.   </script>
  77. </head>

  78. <body>
  79. <div style="width:800px;margin:0 auto;">
  80.     <form action="#" >
  81.      <fieldset>
  82.     <legend>Leave Message Here</legend>
  83.     <table>
  84.         <tr>
  85.             <td>Email:</td>
  86.             <td><input name="email" id="email" type="email" /></td>
  87.         </tr>
  88.         <tr>
  89.             <td>Content:</td>
  90.             <td><textarea name="content" id="content" cols="80" rows="6" ></textarea></td>
  91.         </tr>
  92.         <tr>
  93.             <td></td>
  94.             <td><input name="button1" type="button" value="Post" onclick="doRequestUsingPOST()" /></td>
  95.         </tr>
  96.     </table>
  97.   </fieldset>
  98.  </form>
  99. </div>
  100. <div style="width:800px;margin:0 auto;">
  101.   <hr style="width:800px;margin-bottom:15px;"/>
  102.     <h2>Here are the messages:</h2>
  103. </div>
  104. <div id="newmessage" style="width:800px;margin:0 auto;">
  105.     
  106.     <table style="border:1px gray dash;">
  107.         <?php foreach($data as $message){ ?>
  108.         <tr>
  109.             <th style="background-color:gray;">Email:</th>
  110.             <td><?php echo $message['email']; ?></td>
  111.         </tr>
  112.         <tr>
  113.             <th style="background-color:gray;">Content:</th>
  114.             <td><?php echo $message['content']; ?></td>
  115.         </tr>
  116.         <tr><td colspan="2"></td></tr>
  117.     <?php } ?>
  118.     </table>
  119. </div>
  120. </body>
  121. </html>

message_ajax.php

  1. <?php
  2.       $mysql = new SaeMysql();
  3.         $email = strip_tags($_POST['email']);
  4.         $content = strip_tags($_POST['content']);
  5.         $sql1 = "insert into message values(NULL, '" . $mysql->escape($email) . "' ,'".$mysql->escape($content)."')";
  6.         $mysql->runSql( $sql1 );
  7.     
  8.        
  9.       $sql = "SELECT * FROM `message` order by id desc LIMIT 10";
  10.       $data = $mysql->getData($sql);
  11.       $responseText = "";
  12.      
  13.       foreach($data as $message){
  14.           $onemessage = "
  15.                 

  16.                     

  17.                     

  18.                 

  19.                 

  20.                     

  21.                     

  22.                 
  23. ";
  24.                 $responseText = $responseText.$onemessage;
  25.             }
  26.             $responseText = $responseText."
  27. Email:".$message['email']."
    Content:".$message['content']."
    "
    ;
  28.             echo $responseText;
  29.         
  30.          
  31. ?>


阅读(1087) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

远离繁华2011-10-14 10:36:39