欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

Ajax與mysql數(shù)據(jù)交互如何制作留言板功能

這篇文章將為大家詳細(xì)講解有關(guān)Ajax與MySQL數(shù)據(jù)交互如何制作留言板功能,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

成都創(chuàng)新互聯(lián)公司堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿(mǎn)足客戶(hù)于互聯(lián)網(wǎng)時(shí)代的灌南網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

最近自己做了一個(gè)小demo,實(shí)現(xiàn)了Ajax與MySQL的數(shù)據(jù)交互,js部分用的是jq,后臺(tái)用的是php,數(shù)據(jù)庫(kù)是mysql,過(guò)時(shí)間再來(lái)一個(gè)node+MongoDB版的。

關(guān)于mysql的使用和安裝,就不多講了,自行百度xampp,Apache服務(wù)器和mysql數(shù)據(jù)庫(kù)集成,非常好用。

首先打開(kāi)服務(wù)器和數(shù)據(jù)庫(kù),我這里先建立了一個(gè)“eleven”的數(shù)據(jù)庫(kù),下面建立了一個(gè)叫做microblog的表(請(qǐng)注意:我這里使用的是高版本的mysql,里面php鏈接數(shù)據(jù)庫(kù)的方法使用的都是mysqli_  如果版本過(guò)低,請(qǐng)使用mysql_ 方法,自行修改代碼)
以下是代碼部分:

html頁(yè)面和js部分:

<!DOCTYPE html> 
<html> 
  <head> 
    <meta charset="UTF-8"> 
    <title>微博留言板</title> 
    <style type="text/css"> 
      *{ 
        margin: 0; 
        padding: 0; 
      } 
      #box{ 
        width: 600px; 
        /*height: 500px;*/ 
        border: 2px solid rgb(85,85,85); 
        border-radius: 15px; 
        margin: 50px auto; 
        padding: 20px 10px 15px; 
        background-color: rgb(85,85,85); 
      } 
      #content{ 
        display: block; 
        resize: none; 
        width: 550px; 
        height: 200px; 
        margin: 0 auto; 
        border: 2px solid rgb(225,225,225); 
        border-radius: 10px; 
        text-align: center; 
        font-size: 30px; 
        background-color: rgb(225,225,225); 
      } 
      #content:focus{ 
        outline: none; 
        border: 2px solid rgb(225,225,225); 
        box-shadow: 0 0 15px rgb(225,225,225); 
      } 
      #btn{ 
        border: 2px solid rgb(255,204,0); 
        width: 80px; 
        height: 40px; 
        border-radius: 5px; 
        margin-top: 30px; 
        font-size: 17px; 
        cursor: pointer; 
        outline: none; 
        background-color: rgb(255,204,0); 
      } 
       
      .list{ 
        list-style: none; 
        background-color: rgb(249,249,249); 
        margin-top: 20px; 
      } 
      .list>li{ 
        padding: 20px 10px 10px; 
        border-bottom: 2px solid rgb(68,68,68); 
        font-size: 20px; 
        color: rgb(200,214,225); 
        position: relative; 
        word-break: break-word; 
        word-wrap: break-word; 
        background-color: rgb(85,85,85); 
         
      } 
      .list>li>.control{ 
        position: absolute; 
        bottom: 3px; 
        right: 5px; 
        font-size: 14px; 
      } 
      .list>li>p{ 
        margin-bottom: 25px; 
      } 
      .control span,.control em{ 
        display: inline-block; 
        margin-right: 15px; 
      } 
      .control em{ 
        color: darkblue; 
        cursor: pointer; 
      } 
      a{ 
        text-decoration: none; 
        color: darkred; 
      } 
      #page>a{ 
        display:inline-block; 
        width: 40px; 
        height: 30px; 
        margin-top: 10px; 
        text-align: center; 
        line-height: 30px; 
        font-size: 20px; 
        border-radius: 5px; 
        color: white; 
        background-color: rgb(51,21,70); 
      } 
      #head{ 
        color: rgb(200,214,225); 
        font-size: 30px; 
        height: 50px; 
        border-bottom: 2px solid rgb(68,68,68); 
        margin-bottom: 20px; 
      } 
    </style> 
  </head> 
  <body> 
    <div id="box"> 
      <div id="head"> 
        留言板 
      </div> 
      <div id="fill_in"> 
        <textarea id="content"></textarea> 
        <button id="btn">提交留言</button> 
      </div> 
      <!--留言列表--> 
      <div id="message_text"> 
        <ul class="list"> 
        </ul> 
      </div> 
      <!--分頁(yè)--> 
      <div id="page"> 
        <a href="javasript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" >1</a> 
        <a href="javasript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" >2</a> 
      </div> 
    </div> 
  </body> 
  <script src="Jq/jquery-3.1.1.min.js"> 
</html>

代碼顯示不完,下面是php部分代碼。
接01部分,jq的ajax請(qǐng)求:

<script type="text/javascript"> 
    $(function(){ 
      $("#btn").on("click",function(){ 
        if ($("#content").val() == "") { 
          alert("~~客官,說(shuō)一句再走唄~~"); 
          return; 
        }  
        else{ 
          $.ajax({ 
            type:"get", 
            url:"http://localhost/phpStudy/ajax03/message.php", 
            async:true, 
            dataType:"json", 
            data:{ 
              content:$("#content").val(), 
              act:"add" 
            }, 
            success:function(data){ 
//             var result = JSON.parse(data); 
              if (data.error==0) { 
                createLi(data.id,$("#content").val(),data.time); 
              } else{ 
                alert(data.msg); 
              } 
            } 
          }); 
        } 
         
      }); 
       
      //創(chuàng)建節(jié)點(diǎn) 
      function createLi(id,content,time){ 
        var html = $('<li><p>'+content+'</p><div class="control"><span>時(shí)間:'+time+'</span>頂:<em>0</em>踩:<em>0</em><a class="remove" href="javasript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" >刪除</a></div></li>'); 
          $(".list").prepend(html); 
          var h = html.height(); 
          html.height(0); 
          html.stop().animate({ 
            height:h 
          },300); 
          //刪除 
          html.find(".remove").on("click",function(){ 
            html.stop().animate({ 
              height:0 
            },300,function(){ 
              html.remove(); 
            }) 
          }); 
      } 
    }) 
  </script>

這部分是php代碼部分:

<?php 
  header("Content-type:text/html;charset=utf8"); 
   date_default_timezone_set("PRC"); 
   //鏈接數(shù)據(jù)庫(kù) 
   $link = mysqli_connect("localhost", "root", "", "eleven"); 
   //設(shè)置數(shù)據(jù)庫(kù)編碼格式 
   mysqli_query($link, "set names utf8"); 
?>

注意:這部分我寫(xiě)成了公共代碼,因?yàn)槲以趯W(xué)習(xí)做其他東西時(shí)調(diào)用了,所以下面的代碼會(huì)有

include_once "comment.php";

這一行是引用其他代碼

<?php 
  /* 
   * 開(kāi)發(fā)文檔 
   * 1.用于提交的留言留言進(jìn)行存儲(chǔ) 
   * url:http://localhost/phpStudy/ajax03/message.php 
   * 提交方式:get 
   * 提交參數(shù)說(shuō)明: 
   * content 必須 留言板內(nèi)容 
   * act 必須 借口標(biāo)識(shí) 
   * 返回參數(shù)說(shuō)明 
   * 成功:{"error":"0","id":1,"time":"2016-11-30"} 
   * 失?。簕"error":1,"msg":"留言失敗,請(qǐng)重試"} 
   * 
   * 2.up 
   * id 必須 留言的id編號(hào) 
   * act 必須 接口標(biāo)識(shí) up 
   * 返回聲明 
   * 成功:{"error":"0"} 
   * 失?。簕"error":"1","msg":"點(diǎn)贊失敗"} 
   * 
   * 3.分頁(yè)(獲取頁(yè)碼的接口); 
   * act 必須 接口標(biāo)識(shí) count 
   * 返回參數(shù)說(shuō)明: 
   * 成功:{"error":"0","countPage":"3"}; 
   * 失?。簕"error":"1","msg":"請(qǐng)求數(shù)據(jù)失敗,請(qǐng)重試!"} 
   * 
   * 4.分頁(yè)(點(diǎn)擊頁(yè)碼跳轉(zhuǎn)) 
   * url:http://localhost/phpStudy/ajax03/message.php 
   * 提交方式:get 
   * 提交參數(shù)說(shuō)明: 
   * starPage 必須參數(shù) 頁(yè)碼索引值 
   * act 必須 接口標(biāo)識(shí) page 
   * 返回參數(shù)說(shuō)明 
   * 成功:{"error":"0","data":[{},{},{},{},{}]} 
   * 失?。簕"error":"1","msg":"數(shù)據(jù)查詢(xún)失敗,請(qǐng)重試!"} 
   * 
   */ 
   include_once "comment.php"; 
   $act = $_GET["act"];//接口請(qǐng)求標(biāo)識(shí) 
   switch ($act) { 
    case 'add'://提交留言 
      $content = $_GET["content"]; 
      $time = time(); 
      $times = date("Y-m-d H:i:s",$time); 
      $query = "INSERT INTO microblog(id,content,time) VALUES(null,'{$content}','{$times}')"; 
      mysqli_query($link,$query); 
      $insertId = mysqli_insert_id($link); 
      if($insertId>0){ 
        $arr = ["error"=>0,"id"=>$insertId,"time"=>$times]; 
        echo json_encode($arr);//將數(shù)組轉(zhuǎn)化為json,方便前端使用 
      } 
      else{ 
        $arr = ["error"=>1,"msg"=>"留言失敗,請(qǐng)重試!"]; 
        echo json_encode($arr);//將數(shù)組轉(zhuǎn)化為json,方便前端使用 
      } 
      break; 
       
    case 'up': 
      $id = $_GET['id']; 
      $search = "SELECT up FROM microblog WHERE id = $id"; 
      $result = mysqli_query($link, $search); 
      $upNum = mysqli_fetch_row($result)[0]; 
      $upNum++; 
      $query = "UPDATE microblog SET up='{$upNum}' WHERE id = '{$id}'"; 
      mysqli_query($link,$query); 
      if(mysqli_affected_rows($link)){//更新數(shù)據(jù)成功 
        echo '{"error":"0"}'; 
      } 
      else{//更新失敗 
        echo '{"error":"1","msg":"點(diǎn)贊失敗!"}'; 
      } 
      break; 
       
    case 'down': 
      $id = $_GET['id']; 
      $search = "SELECT down FROM microblog WHERE id = $id"; 
      $result = mysqli_query($link, $search); 
      $downNum = mysqli_fetch_row($result)[0]; 
      $downNum++; 
      $query = "UPDATE microblog SET down='{$downNum}' WHERE id = '{$id}'"; 
      mysqli_query($link,$query); 
      if(mysqli_affected_rows($link)){//更新數(shù)據(jù)成功 
        echo '{"error":"0"}'; 
      } 
      else{//更新失敗 
        echo '{"error":"1","msg":"踩失??!"}'; 
      } 
      break;  
    case 'remove': 
      $id = $_GET['id']; 
      $query ="DELETE FROM microblog WHERE id='{$id}'"; 
      mysqli_query($link,$query); 
      if(mysqli_affected_rows($link)>0){//刪除數(shù)據(jù)成功 
        echo '{"error":"0"}'; 
      } 
      else{ 
        echo '{"error":"1","msg":"刪除失?。?quot;}'; 
      } 
      break; 
    case 'count'://返回總頁(yè)碼 
      $query = "SELECT count(id) FROM   microblog"; 
      $result = mysqli_query($link, $query); 
      $count = mysqli_fetch_row($result)[0];//以索引數(shù)組形式返回查詢(xún)結(jié)果 
      $countPage = ceil($count/5); 
      echo '{"error":"0","countPage":"'.$countPage.'"}'; 
      break;  
    case 'page'://點(diǎn)擊分頁(yè)或者是頁(yè)面第一次加載 
      $index = $_GET["num"]*5; 
      $search = "SELECT * FROM microblog ORDER BY id DESC LIMIT {$index},5";//倒敘查詢(xún)留言 
      $result = mysqli_query($link, $search); 
      $arr = [];//存查詢(xún)出來(lái)的數(shù)據(jù) 
      while($row = mysqli_fetch_assoc($result)){ 
        array_unshift($arr,$row); 
      } 
//     print_r($arr); 
//     {"error":"0","info":[{},{},{},{},{}]} 
      $resultArr = ["error"=>"0","info"=>$arr]; 
      echo json_encode($resultArr); 
      break; 
     
   } 
?>

關(guān)于“Ajax與mysql數(shù)據(jù)交互如何制作留言板功能”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

分享名稱(chēng):Ajax與mysql數(shù)據(jù)交互如何制作留言板功能
文章源于:http://chinadenli.net/article40/gohheo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、搜索引擎優(yōu)化、網(wǎng)站導(dǎo)航網(wǎng)站內(nèi)鏈、響應(yīng)式網(wǎng)站、品牌網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)
嫩草国产福利视频一区二区| 国产成人精品综合久久久看| 99热九九热这里只有精品| 国产免费自拍黄片免费看| 麻豆最新出品国产精品| 一区二区在线激情视频| 亚洲日本中文字幕视频在线观看| 国产在线观看不卡一区二区| 日韩av生活片一区二区三区| 高潮少妇高潮久久精品99| 日韩精品中文字幕在线视频| 免费观看潮喷到高潮大叫| 在线免费视频你懂的观看| 一区二区三区四区亚洲专区| 国产精品不卡高清在线观看| 欧美午夜一级特黄大片| 激情丁香激情五月婷婷| 亚洲av秘片一区二区三区| 免费在线播放不卡视频| 精品国模一区二区三区欧美| 激情综合五月开心久久| 日本精品中文字幕人妻| 日韩欧美综合中文字幕| 亚洲中文字幕在线观看四区| 国产精品亚洲一区二区| 日本高清不卡一二三区| 大尺度激情福利视频在线观看| 国产精品福利一级久久| 亚洲另类女同一二三区| 久久久精品日韩欧美丰满| 欧美成人免费视频午夜色| 日韩专区欧美中文字幕| 人妻熟女中文字幕在线| 黄片在线观看一区二区三区| 亚洲国产中文字幕在线观看| 亚洲一区二区精品福利| 久久永久免费一区二区| 激情爱爱一区二区三区| 五月情婷婷综合激情综合狠狠| 日韩欧美精品一区二区三区| 国产精品日本女优在线观看|