<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當(dāng)前位置: 首頁 - 科技 - 知識百科 - 正文

        Ajax+mysql做出留言板的功能

        來源:懂視網(wǎng) 責(zé)編:小采 時間:2020-11-27 19:56:58
        文檔

        Ajax+mysql做出留言板的功能

        Ajax+mysql做出留言板的功能:這次給大家?guī)鞟jax+mysql做出留言板的功能,Ajax+mysql做出留言板的注意事項有哪些,下面就是實戰(zhàn)案例,一起來看一下。最近自己做了一個小demo,實現(xiàn)了Ajax與MySQL的數(shù)據(jù)交互,js部分用的是jq,后臺用的是php,數(shù)據(jù)庫是mysql,過時間再來一個node+
        推薦度:
        導(dǎo)讀Ajax+mysql做出留言板的功能:這次給大家?guī)鞟jax+mysql做出留言板的功能,Ajax+mysql做出留言板的注意事項有哪些,下面就是實戰(zhàn)案例,一起來看一下。最近自己做了一個小demo,實現(xiàn)了Ajax與MySQL的數(shù)據(jù)交互,js部分用的是jq,后臺用的是php,數(shù)據(jù)庫是mysql,過時間再來一個node+

        這次給大家?guī)鞟jax+mysql做出留言板的功能,Ajax+mysql做出留言板的注意事項有哪些,下面就是實戰(zhàn)案例,一起來看一下。

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

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

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

        html頁面和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> 
         <p id="box"> 
         <p id="head"> 
         留言板 
         </p> 
         <p id="fill_in"> 
         <textarea id="content"></textarea> 
         <button id="btn">提交留言</button> 
         </p> 
         <!--留言列表--> 
         <p id="message_text"> 
         <ul class="list"> 
         </ul> 
         </p> 
         <!--分頁--> 
         <p 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> 
         </p> 
         </p> 
         </body> 
         <script src="Jq/jquery-3.1.1.min.js"> 
        </html>

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

        <script type="text/javascript"> 
         $(function(){ 
         $("#btn").on("click",function(){ 
         if ($("#content").val() == "") { 
         alert("~~客官,說一句再走唄~~"); 
         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é)點 
         function createLi(id,content,time){ 
         var html = $('<li><p>'+content+'</p><p class="control"><span>時間:'+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></p></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ù)庫 
         $link = mysqli_connect("localhost", "root", "", "eleven"); 
         //設(shè)置數(shù)據(jù)庫編碼格式 
         mysqli_query($link, "set names utf8"); 
        ?>

        注意:這部分我寫成了公共代碼,因為我在學(xué)習(xí)做其他東西時調(diào)用了,所以下面的代碼會有

        include_once "comment.php";

        這一行是引用其他代碼

        <?php 
         /* 
         * 開發(fā)文檔 
         * 1.用于提交的留言留言進(jìn)行存儲 
         * url:http://localhost/phpStudy/ajax03/message.php 
         * 提交方式:get 
         * 提交參數(shù)說明: 
         * content 必須 留言板內(nèi)容 
         * act 必須 借口標(biāo)識 
         * 返回參數(shù)說明 
         * 成功:{"error":"0","id":1,"time":"2016-11-30"} 
         * 失敗:{"error":1,"msg":"留言失敗,請重試"} 
         * 
         * 2.up 
         * id 必須 留言的id編號 
         * act 必須 接口標(biāo)識 up 
         * 返回聲明 
         * 成功:{"error":"0"} 
         * 失敗:{"error":"1","msg":"點贊失敗"} 
         * 
         * 3.分頁(獲取頁碼的接口); 
         * act 必須 接口標(biāo)識 count 
         * 返回參數(shù)說明: 
         * 成功:{"error":"0","countPage":"3"}; 
         * 失敗:{"error":"1","msg":"請求數(shù)據(jù)失敗,請重試!"} 
         * 
         * 4.分頁(點擊頁碼跳轉(zhuǎn)) 
         * url:http://localhost/phpStudy/ajax03/message.php 
         * 提交方式:get 
         * 提交參數(shù)說明: 
         * starPage 必須參數(shù) 頁碼索引值 
         * act 必須 接口標(biāo)識 page 
         * 返回參數(shù)說明 
         * 成功:{"error":"0","data":[{},{},{},{},{}]} 
         * 失敗:{"error":"1","msg":"數(shù)據(jù)查詢失敗,請重試!"} 
         * 
         */ 
         include_once "comment.php"; 
         $act = $_GET["act"];//接口請求標(biāo)識 
         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"=>"留言失敗,請重試!"]; 
         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":"點贊失敗!"}'; 
         } 
         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":"刪除失敗!"}'; 
         } 
         break; 
         case 'count'://返回總頁碼 
         $query = "SELECT count(id) FROM microblog"; 
         $result = mysqli_query($link, $query); 
         $count = mysqli_fetch_row($result)[0];//以索引數(shù)組形式返回查詢結(jié)果 
         $countPage = ceil($count/5); 
         echo '{"error":"0","countPage":"'.$countPage.'"}'; 
         break; 
         case 'page'://點擊分頁或者是頁面第一次加載 
         $index = $_GET["num"]*5; 
         $search = "SELECT * FROM microblog ORDER BY id DESC LIMIT {$index},5";//倒敘查詢留言 
         $result = mysqli_query($link, $search); 
         $arr = [];//存查詢出來的數(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; 
         
         } 
        ?>

        相信看了本文案例你已經(jīng)掌握了方法,更多精彩請關(guān)注Gxl網(wǎng)其它相關(guān)文章!

        推薦閱讀:

        用Ajax實現(xiàn)同步和異步有什么區(qū)別

        AJAX跨域請求JSONP獲取JSON數(shù)據(jù)步驟詳解(附代碼)

        聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        Ajax+mysql做出留言板的功能

        Ajax+mysql做出留言板的功能:這次給大家?guī)鞟jax+mysql做出留言板的功能,Ajax+mysql做出留言板的注意事項有哪些,下面就是實戰(zhàn)案例,一起來看一下。最近自己做了一個小demo,實現(xiàn)了Ajax與MySQL的數(shù)據(jù)交互,js部分用的是jq,后臺用的是php,數(shù)據(jù)庫是mysql,過時間再來一個node+
        推薦度:
        標(biāo)簽: 留言板 mysql mysql的
        • 熱門焦點

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 美女裸免费观看网站| 亚洲国产区男人本色在线观看| 全黄A免费一级毛片| 亚洲蜜芽在线精品一区| A片在线免费观看| 亚洲成AV人片在线观看| 美女被cao网站免费看在线看| 国产av无码专区亚洲av桃花庵 | 日韩精品成人亚洲专区| 羞羞漫画在线成人漫画阅读免费| 又大又黄又粗又爽的免费视频| 免费播放美女一级毛片| 久久青青草原亚洲av无码| 美女视频黄的免费视频网页| 亚洲国产精品久久久久| 18禁成人网站免费观看| 国产成人精品日本亚洲直接| 日本高清免费不卡在线| 日韩在线视频播放免费视频完整版| 亚洲精品色婷婷在线影院| 野花香高清视频在线观看免费| 久久亚洲国产精品| 一区二区三区精品高清视频免费在线播放| 又粗又大又猛又爽免费视频| 中文字幕乱码系列免费| 777亚洲精品乱码久久久久久| 无人在线观看完整免费版视频| 久久久久亚洲AV无码去区首| 亚洲中文字幕无码一区二区三区| 亚洲一区免费观看| 久久久久久久尹人综合网亚洲| 91免费在线播放| 羞羞漫画登录页面免费| 亚洲成人精品久久| 永久免费无码网站在线观看| 亚洲一级毛片免观看| 亚洲国产精品自在拍在线播放| 999任你躁在线精品免费不卡| 理论片在线观看免费| 亚洲视频在线观看不卡| 亚洲国产天堂久久综合|