php 웹 페이지 채 팅 소프트웨어 구현 코드
1. index.html
<html>
 <head>
  <title>   </title>
  <meta charset="utf-8"/>
  <link href="http://libs.baidu.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet">
  <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
  <script src="http://libs.baidu.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>
  <link href="./css/style.css" rel="stylesheet"/>
  <script>
   var maxid = 0;
   function showmessage() {
    //  ajax  
    var xhr = new XMLHttpRequest();
    //  
    xhr.onreadystatechange = function () {
     if (xhr.readyState === 4) {
      eval("var info=" + xhr.responseText);
      var text = "";
      for (var i = 0; i < info.length; i++)
      {
       text += "<div class='alert alert-success'>" +
         "<span class='name'>" + info[i].send + ":</span>" +
         "<span class='message'>" + info[i].content + "</span>" +
         "<span>(" + info[i].time + ")</span>" +
         "</div>";
       maxid = info[i].id;
      }
 
      var old = document.getElementById("msg").innerHTML;
      document.getElementById("msg").innerHTML = old + text;
      document.getElementById("msg").scrollTop = document.getElementById("msg").scrollHeight;
     }
    };
    //   
    xhr.open("get", "./action.php?maxid=" + maxid);
    //  
    xhr.send();
   }
 
   $(document).ready(
     function ()
     {
      showmessage()
      self.setInterval("showmessage()", 2000);
     }
   );
   function send() {
    var postData ="content="+document.getElementById('content').value;
    var xhr = new XMLHttpRequest();
    xhr.open("POST", "./add.php", true);
    //                
    xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xhr.onreadystatechange = function () {
     var XMLHttpReq = xhr;
     if (XMLHttpReq.readyState == 4) {
      if (XMLHttpReq.status == 200) {
       document.getElementById('content').value="";
      }
     }
    };
    xhr.send(postData);
   }
  </script>
 </head>
 <body>
 
  <div class="panel panel-default" id="main">
   <!--     --> 
   <div class="panel-heading">
    <h3 class="panel-title">
           
    </h3>
   </div>
   <!--     -->
   <!--      -->
   <div class="panel-body ">
    <div class="well no-bottom ">
     <!--          -->
     <!--
     <div class="alert alert-success">  !        。</div>
     <div class="alert alert-info">  !       。</div>
     <div class="alert alert-warning">  !     。</div>
     <div class="alert alert-danger">  !       。</div>
     -->
     <div id="msg" class="showmessage">
     </div>
     <!--      -->
     <div class="well" >
      <form role="form">
       <div class="form-group">
        <label for="name">    </label>
        <textarea class="form-control" id="content" name="content"
           style="resize: none;font-family: Microsoft YaHei;" rows="3" >
        </textarea>
       </div>
       <div style="text-align: right">
        <button type="button" class="btn btn-primary" onclick="send()">
         <span class="glyphicon glyphicon-envelope"></span>
           
        </button>
       </div>
      </form>
     </div>
     <!--      -->
    </div>
   </div>
  </div>
 </body>
</html>
<?php
 
$link = mysqli_connect('localhost', 'root', '123', 'test');
mysqli_query($link, 'set names utf8');
$info= array();
header("Content-type: text/html; charset=utf-8");
$id=$_GET['maxid'];
$data = mysqli_query($link, "select * from talk where id>$id");
while ($array = mysqli_fetch_assoc($data)) {
 $info[] = $array;
};
 
 echo json_encode($info);
DROP TABLE IF EXISTS `talk`;
CREATE TABLE `talk` (
 `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'id',
 `send` varchar(10) CHARACTER SET utf8 DEFAULT NULL COMMENT '     ',
 `ip` varchar(12) CHARACTER SET utf8 DEFAULT NULL,
 `content` varchar(500) CHARACTER SET utf8 DEFAULT NULL,
 `time` varchar(50) DEFAULT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=52 DEFAULT CHARSET=utf8;
<?php
 
/**
 * @           
 */
$content=$_POST['content'];
print_r($_POST);
$time= date("Y-m-d H:i:s", time(0));
$link = mysqli_connect('localhost', 'root', '123', 'test');
mysqli_query($link, 'set names utf8');
$ip = $_SERVER["REMOTE_ADDR"];
$sql="INSERT INTO talk VALUES(NULL,'   ','$ip','$content','$time')";
$data = mysqli_query($link,$sql);
echo "$content";
if($data)
 echo "1";
 else
  echo "0";이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Laravel - 변환된 유효성 검사 규칙으로 API 요청 제공동적 콘텐츠를 위해 API를 통해 Laravel CMS에 연결하는 모바일 앱(또는 웹사이트) 구축을 고려하십시오. 이제 앱은 CMS에서 번역된 콘텐츠를 받을 것으로 예상되는 다국어 앱이 될 수 있습니다. 일반적으로 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.