亚洲最大看欧美片,亚洲图揄拍自拍另类图片,欧美精品v国产精品v呦,日本在线精品视频免费

  • 站長(zhǎng)資訊網(wǎng)
    最全最豐富的資訊網(wǎng)站

    php如何實(shí)現(xiàn)無(wú)刷新點(diǎn)贊

    php實(shí)現(xiàn)無(wú)刷新點(diǎn)贊的方法:首先通過(guò)ajax index.php點(diǎn)擊按鈕;然后實(shí)現(xiàn)js反應(yīng);接著通過(guò)ajax異步提交給“sever.php”;最后通過(guò)js返回給頁(yè)面即可實(shí)現(xiàn)無(wú)刷新點(diǎn)贊。

    php如何實(shí)現(xiàn)無(wú)刷新點(diǎn)贊

    推薦:《PHP視頻教程》

    ajax+php+mysql實(shí)現(xiàn)無(wú)刷新點(diǎn)贊功能

    php如何實(shí)現(xiàn)無(wú)刷新點(diǎn)贊

    從動(dòng)態(tài)圖看出來(lái),點(diǎn)擊贊的按鈕的時(shí)候,旁邊的贊數(shù)量在無(wú)刷新地增加。打開(kāi)數(shù)據(jù)庫(kù)也能看到贊數(shù)量更新了。

    原理就是通過(guò)ajax異步提交數(shù)據(jù)給數(shù)據(jù)庫(kù)。

    首先前端頁(yè)面就是一個(gè)按鈕和贊數(shù)量。

    數(shù)據(jù)庫(kù)名,test,表名zan,字段zan

    ajaxindex.php

    <!DOCTYPE html> <html> <head>     <meta charset="utf-8">     <title>異步提交</title>     <script src="jquery-2.1.1.min.js"></script>     <script src="ajaxindex.js"></script> </head> <body>     <button id="btn">贊</button>     <span id="result">         <?php         $con = mysql_connect("localhost","root","root");         if (!$con)         {         die('連接數(shù)據(jù)庫(kù)失敗,失敗原因:' . mysql_error());         }         //設(shè)置數(shù)據(jù)庫(kù)字符集           mysql_query("SET NAMES UTF8");         //查詢數(shù)據(jù)庫(kù)         mysql_select_db("test", $con);         $result = mysql_query("SELECT * FROM zan");         while($row = mysql_fetch_array($result))         {         echo $row['zan'];         }         //關(guān)閉連接         mysql_close($con);         ?>     </span> </body> </html> ajaxindex.js $(document).ready(function(){     $("#btn").on("click",function(){         $.get("sever.php",{name:$("#btn").val()},function(data){             $("#result").text(data);         });     }); });

    sever.php

    <?php header("Content-type:text/html;charset=utf-8"); //連接數(shù)據(jù)庫(kù) $con = mysql_connect("localhost","root","root"); if (!$con)   {   die('連接數(shù)據(jù)庫(kù)失敗,失敗原因:' . mysql_error());   } //設(shè)置數(shù)據(jù)庫(kù)字符集   mysql_query("SET NAMES UTF8"); //查詢數(shù)據(jù)庫(kù) mysql_select_db("test", $con); //更新 mysql_query("UPDATE zan SET zan = zan+1"); $result = mysql_query("SELECT * FROM zan"); if(isset($_GET['name'])){ while($row = mysql_fetch_array($result))   {   echo $row['zan'];   } }else{     echo "贊失敗!"; } //關(guān)閉連接 mysql_close($con); ?>

    總體思路:

    通過(guò)ajaxindex.php點(diǎn)擊按鈕,js反應(yīng),ajax異步提交給sever.php再通過(guò)js返回給頁(yè)面,就不用刷新了。

    sever.php就是一個(gè)查詢和更新數(shù)據(jù)的,更新之后再把數(shù)據(jù)輸出給頁(yè)面。

    整個(gè)demo下載:https://pan.lanzou.com/1485785

    贊(0)
    分享到: 更多 (0)
    網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)