微信小程序學習筆記之表單提交與PHP后臺數(shù)據(jù)交互處理圖文詳解
本文實例講述了微信小程序學習筆記之表單提交與PHP后臺數(shù)據(jù)交互處理。分享給大家供大家參考,具體如下:
前面一篇結介紹了微信小程序函數(shù)定義、頁面渲染。這里介紹form表單提交與后臺php數(shù)據(jù)交互處理。
【form表單提交】
form.wxml:
<form bindsubmit="formSubmit" bindreset="formReset"> <view> 昵稱:<input type="text" name="nickname" placeholder="請輸入昵稱" confirm-type="done" /> 密碼:<input password type="number" name="password" placeholder="請輸入6位密碼" maxlength="6" /> 性別: <radio-group name="sex"> <label><radio value="女"/>女</label> <label><radio value="男"/>男</label> </radio-group> 愛好: <checkbox-group name="aihao"> <label><checkbox value="cy"/>抽煙</label> <label><checkbox value="hj"/>喝酒</label> <label><checkbox value="tt"/>燙頭</label> </checkbox-group> 狀態(tài):<switch name="status"/> <view>成績:<slider name="grade" show-value ></slider></view> </view> <view class="btn-area"> <button formType="submit">提交</button> <button formType="reset">重置</button> </view> </form>
form.js:
Page({ formSubmit: function (e) { console.log('form發(fā)生了submit事件,提交數(shù)據(jù):', e.detail.value) }, formReset: function () { console.log('form發(fā)生了reset事件') } })
提交觸發(fā)formSubmit:
重置觸發(fā)formReset:
【表單數(shù)據(jù)提交到PHP后臺服務器】
使用 wx.request API發(fā)送HTTPS請求
前臺form.js:
Page({ formSubmit: function (e) { wx.request({ url: 'https://www.msllws.top/getdata.php', data: { 'nickname': e.detail.value.nickname, 'password': e.detail.value.password, 'sex': e.detail.value.sex, 'status': e.detail.value.status, 'aihao': e.detail.value.aihao, 'grade': e.detail.value.grade }, method:'POST', header: { 'Content-Type': 'application/x-www-form-urlencoded' }, success: function (res) { console.log(res.data) } }) } })
后臺接口getdata.php:
<?php $postdata = $_POST; //獲得POST請求提交的數(shù)據(jù) //打印日志 方便查看 $fp = fopen('./log.txt','a+'); fwrite($fp,var_export($postdata,true)); fclose($fp); echo 666; //返回狀態(tài)或數(shù)據(jù)
提交后日志文件log.txt內容如下,這些就是PHP后臺獲得的數(shù)據(jù),可以對其進行數(shù)據(jù)庫操作:
array ( 'nickname' => '李棟', 'password' => '123456', 'sex' => '男', 'status' => 'true', 'aihao' => 'cy,hj,tt', 'grade' => '66', )

【PHP后臺對提交過來的數(shù)據(jù)進行判斷、處理,返回狀態(tài),前臺小程序給出提示】
示例如下,如果輸入名字提示提交成功,不輸入名字提示名字為空。
后臺接口getdata.php:
<?php $postdata = $_POST; $fp = fopen('./log.txt','a+'); fwrite($fp,var_export($postdata,true)); fclose($fp); if($postdata['nickname']){ $arr['state'] = 1; $arr['info'] = '提交成功'; }else{ $arr['state'] = 0; $arr['info'] = '名字為空'; } echo json_encode($arr);die;
前臺form.js:
Page({ formSubmit: function (e) { wx.request({ url: 'https://www.msllws.top/getdata.php', data: { 'nickname': e.detail.value.nickname, 'password': e.detail.value.password, 'sex': e.detail.value.sex, 'status': e.detail.value.status, 'aihao': e.detail.value.aihao, 'grade': e.detail.value.grade }, method: 'POST', header: { 'Content-Type': 'application/x-www-form-urlencoded' }, success: function (res) { if (res.data.state == 1) { wx.showToast({ title: res.data.info }); }else{ wx.showToast({ title: res.data.info }); } } }) } })
【請求PHP后臺API接口,獲得數(shù)據(jù),渲染頁面】
示例如下,獲得10條博客信息顯示在頁面中(接口用tp5寫的,普通php文件用echo json_encode();
返回數(shù)據(jù))。
后臺接口Getdata.php:
<?php namespace app\home\controller; use think\Controller; class Getdata extends Controller { public function index() { //查詢10篇博客 $whe['is_del'] = 'N'; $artinfo = db('article')->field('`article_id`,`article_title`,`thumbnail`')->where($whe)->limit(10)->select(); //拼接縮略圖路徑 foreach ($artinfo as $k => $v) { $artinfo[$k]['thumbnail'] = 'https://www.msllws.top'.$v['thumbnail']; } return json($artinfo); } }
前臺data.js:
Page({ onLoad: function () { var that = this wx.request({ url: 'https://www.msllws.top/Getdata', headers: { 'Content-Type': 'application/json' }, success: function (res) { that.setData({ artinfo: res.data }) } }) } })
前臺data.wxml:
<view wx:for="{{artinfo}}" wx:for-item="artinfo"> <view>{{artinfo.article_title}}</view> <image src="{{artinfo.thumbnail}}"></image> </view>
頁面加載,顯示如下:
希望本文所述對大家微信小程序開發(fā)有所幫助。
相關文章
js控件Kindeditor實現(xiàn)圖片自動上傳功能
這篇文章主要為大家詳細介紹了js控件Kindeditor實現(xiàn)圖片自動上傳功能的相關資料,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-06-06javaScript給元素添加多個class的簡單實現(xiàn)
下面小編就為大家?guī)硪黄猨avaScript給元素添加多個class的簡單實現(xiàn)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-07-07