TP5(thinkPHP5)框架使用ajax實(shí)現(xiàn)與后臺數(shù)據(jù)交互的方法小結(jié)
本文實(shí)例講述了TP5(thinkPHP5)框架使用ajax實(shí)現(xiàn)與后臺數(shù)據(jù)交互的方法。分享給大家供大家參考,具體如下:
方法一: serialize() 方法通過序列化表單值,創(chuàng)建 URL 編碼文本字符串,這個(gè)是jquery提供的方法
前端代碼
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>ajax交互</title> <script src="http://cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script> <script> $('.but').click(function () { var formData = $("#myform").serialize();//formData值:account=sdf&passwd=sdf //serialize() 方法通過序列化表單值,創(chuàng)建 URL 編碼文本字符串,這個(gè)是jquery提供的方法 $.ajax({ type: "post", url: "{:url('index/index/reg')}", //數(shù)據(jù)傳輸?shù)目刂破鞣椒? data: formData,//這里data傳遞過去的是序列化以后的字符串 success: function (data) { console.log(data); $("#content").append(data);//獲取成功以后輸出返回值 } }); return false; }) </script> </head> <body> <form id="myform"> <!--這里給表單起個(gè)id用于獲取表單并序列化--> <input type="text" name="account" /> <input type="password" name="passwd" /> <input type="button" value="提交" class="but"> </form> <div id="content"> </div> </body> </html>
后端代碼
public function reg($account,$passwd){ if($account == '123'){ return json("ajax成功!".$account."---".$passwd); }else{ return json("你輸出的是其他值:".$account."---".$passwd); } }
方法二: 利用layui的form.on事件監(jiān)聽
前端代碼
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>管理員登錄</title> <meta name="renderer" content="webkit|ie-comp|ie-stand"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0"> <meta http-equiv="Cache-Control" content="no-siteapp"/> <link rel="shortcut icon" href="./favicon.ico" rel="external nofollow" type="image/x-icon"/> <link rel="stylesheet" href="./static/css/font.css" rel="external nofollow" > <link rel="stylesheet" href="./static/css/weadmin.css" rel="external nofollow" > <script src="./lib/layui/layui.js" charset="utf-8"></script> </head> <body class="login-bg"> <div class="login"> <div class="message">管理登錄</div> <div id="darkbannerwrap"></div> <form method="post" class="layui-form"> <input name="username" placeholder="用戶名" type="text" lay-verify="required" class="layui-input"> <hr class="hr15"> <input name="password" lay-verify="required" placeholder="密碼" type="password" class="layui-input"> <hr class="hr15"> <input class="loginin" value="登錄" lay-submit lay-filter="login" style="width:100%;" type="submit"> <hr class="hr20"> </form> </div> <script src="./static/js/jquery-3.3.1.min.js"></script> <script type="text/javascript"> layui.extend({ admin: '{/}./static/js/admin' }); //layui.use調(diào)用模塊 layui.use(['form', 'admin'], function () { //獲得form模塊 var form = layui.form , admin = layui.admin; //監(jiān)聽提交 //事件監(jiān)聽 //語法:form.on('event(過濾器值)', callback);(過濾器值指lay-filter="過濾器值") //function(data)里的data是一個(gè)object,data.field是表單填寫的內(nèi)容 form.on('submit(login)', function (data) { //$.post寫法:$(selector).post(URL,data,function(data,status,xhr),dataType) var post_data = data.field; $.post("{:url('verify')}" , post_data , function (data) { console.log(data); } ) return false; }); }) ; </script> <!-- 底部結(jié)束 --> </body>
后端代碼
public function verify() { $posts = input("post.password"); return json($posts); }
更多關(guān)于thinkPHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《ThinkPHP入門教程》、《thinkPHP模板操作技巧總結(jié)》、《ThinkPHP常用方法總結(jié)》、《codeigniter入門教程》、《CI(CodeIgniter)框架進(jìn)階教程》、《Zend FrameWork框架入門教程》及《PHP模板技術(shù)總結(jié)》。
希望本文所述對大家基于ThinkPHP框架的PHP程序設(shè)計(jì)有所幫助。
- thinkphp5 + ajax 使用formdata提交數(shù)據(jù)(包括文件上傳) 后臺返回json完整實(shí)例
- ThinkPHP5.1+Ajax實(shí)現(xiàn)的無刷新分頁功能示例
- ThinkPHP5 通過ajax插入圖片并實(shí)時(shí)顯示(完整代碼)
- Thinkphp5框架ajax接口實(shí)現(xiàn)方法分析
- thinkPHP+mysql+ajax實(shí)現(xiàn)的仿百度一下即時(shí)搜索效果詳解
- ThinkPHP框架結(jié)合Ajax實(shí)現(xiàn)用戶名校驗(yàn)功能示例
- thinkPHP利用ajax異步上傳圖片并顯示、刪除的示例
- TP5(thinkPHP5)框架基于ajax與后臺數(shù)據(jù)交互操作簡單示例
- ThinkPHP 5 AJAX跨域請求頭設(shè)置實(shí)現(xiàn)過程解析
相關(guān)文章
PHP靜態(tài)調(diào)用非靜態(tài)方法的應(yīng)用分析
本篇文章介紹了,PHP靜態(tài)調(diào)用非靜態(tài)方法的應(yīng)用分析。需要的朋友參考下2013-05-05php實(shí)現(xiàn)購物車產(chǎn)品刪除功能(2)
這篇文章主要為大家詳細(xì)介紹了php實(shí)現(xiàn)購物車產(chǎn)品刪除功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03PHPStrom 新建FTP項(xiàng)目以及在線操作教程
PhpStorm是一個(gè)輕量級且便捷的PHP IDE,其提供的智能代碼補(bǔ)全,快速導(dǎo)航以及即時(shí)錯(cuò)誤檢查等功能大大提高了編碼效率。它是一款商業(yè)的 PHP 集成開發(fā)工具,以其獨(dú)特的開發(fā)便利性,短時(shí)間內(nèi)贏得了大量PHPer的青睞。今天我們來詳細(xì)學(xué)習(xí)下FTP相關(guān)的操作2016-10-10laravel修改用戶模塊的密碼驗(yàn)證實(shí)現(xiàn)
本文主要介紹了laravel修改用戶模塊的密碼驗(yàn)證實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09