微信小程序?qū)W習(xí)筆記之表單提交與PHP后臺(tái)數(shù)據(jù)交互處理圖文詳解
本文實(shí)例講述了微信小程序?qū)W習(xí)筆記之表單提交與PHP后臺(tái)數(shù)據(jù)交互處理。分享給大家供大家參考,具體如下:
前面一篇結(jié)介紹了微信小程序函數(shù)定義、頁面渲染。這里介紹form表單提交與后臺(tái)php數(shù)據(jù)交互處理。
【form表單提交】
form.wxml:
<form bindsubmit="formSubmit" bindreset="formReset"> <view> 昵稱:<input type="text" name="nickname" placeholder="請(qǐng)輸入昵稱" confirm-type="done" /> 密碼:<input password type="number" name="password" placeholder="請(qǐng)輸入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>成績(jī):<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后臺(tái)服務(wù)器】
使用 wx.request API發(fā)送HTTPS請(qǐng)求
前臺(tái)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)
}
})
}
})
后臺(tái)接口getdata.php:
<?php
$postdata = $_POST; //獲得POST請(qǐng)求提交的數(shù)據(jù)
//打印日志 方便查看
$fp = fopen('./log.txt','a+');
fwrite($fp,var_export($postdata,true));
fclose($fp);
echo 666; //返回狀態(tài)或數(shù)據(jù)
提交后日志文件log.txt內(nèi)容如下,這些就是PHP后臺(tái)獲得的數(shù)據(jù),可以對(duì)其進(jìn)行數(shù)據(jù)庫操作:
array ( 'nickname' => '李棟', 'password' => '123456', 'sex' => '男', 'status' => 'true', 'aihao' => 'cy,hj,tt', 'grade' => '66', )

【PHP后臺(tái)對(duì)提交過來的數(shù)據(jù)進(jìn)行判斷、處理,返回狀態(tài),前臺(tái)小程序給出提示】
示例如下,如果輸入名字提示提交成功,不輸入名字提示名字為空。
后臺(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;
前臺(tái)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
});
}
}
})
}
})

【請(qǐng)求PHP后臺(tái)API接口,獲得數(shù)據(jù),渲染頁面】
示例如下,獲得10條博客信息顯示在頁面中(接口用tp5寫的,普通php文件用echo json_encode();返回?cái)?shù)據(jù))。
后臺(tái)接口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);
}
}
前臺(tái)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
})
}
})
}
})
前臺(tái)data.wxml:
<view wx:for="{{artinfo}}" wx:for-item="artinfo">
<view>{{artinfo.article_title}}</view>
<image src="{{artinfo.thumbnail}}"></image>
</view>
頁面加載,顯示如下:

希望本文所述對(duì)大家微信小程序開發(fā)有所幫助。
- 微信小程序開發(fā)之獲取用戶手機(jī)號(hào)碼(php接口解密)
- 微信小程序發(fā)送訂閱消息的方法(php 為例)
- 基于PHP實(shí)現(xiàn)微信小程序客服消息功能
- PHP小程序支付功能完整版【基于thinkPHP】
- PHP實(shí)現(xiàn)微信小程序用戶授權(quán)的工具類示例
- PHP后臺(tái)實(shí)現(xiàn)微信小程序登錄
- 微信小程序調(diào)用PHP后臺(tái)接口 解析純html文本
- 微信小程序圖片選擇、上傳到服務(wù)器、預(yù)覽(PHP)實(shí)現(xiàn)實(shí)例
- 微信小程序 PHP后端form表單提交實(shí)例詳解
- PHP:微信小程序 微信支付服務(wù)端集成實(shí)例詳解及源碼下載
- PHP小程序后臺(tái)部署運(yùn)行 LNMP+WNMP的方法
相關(guān)文章
js網(wǎng)頁側(cè)邊隨頁面滾動(dòng)廣告效果實(shí)現(xiàn)
其實(shí)這個(gè)效果不是什么難實(shí)現(xiàn)的效果,關(guān)鍵注意幾個(gè)地方就可以了2011-04-04
JavaScript設(shè)計(jì)模式之裝飾者模式介紹
這篇文章主要介紹了JavaScript設(shè)計(jì)模式之裝飾者模式介紹,通一個(gè)類來動(dòng)態(tài)的對(duì)另一個(gè)類的功能對(duì)象進(jìn)行前或后的修飾,給它輔加一些額外的功能; 這是對(duì)一個(gè)類對(duì)象功能的裝飾,裝飾的類跟被裝飾的類,要求擁有相同的訪問接口方法(功能),需要的朋友可以參考下2014-12-12
JS window對(duì)象簡(jiǎn)單操作完整示例
這篇文章主要介紹了JS window對(duì)象簡(jiǎn)單操作,結(jié)合完整實(shí)例形式分析了JavaScript Window對(duì)象各種常見提示框、彈出窗口及時(shí)間相關(guān)操作技巧,需要的朋友可以參考下2020-01-01
js控件Kindeditor實(shí)現(xiàn)圖片自動(dòng)上傳功能
這篇文章主要為大家詳細(xì)介紹了js控件Kindeditor實(shí)現(xiàn)圖片自動(dòng)上傳功能的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06
JavaScript組合模式學(xué)習(xí)要點(diǎn)
組合模式大概是設(shè)計(jì)模式里面使用最為廣泛的模式之一了,模式本身理解起來也比較簡(jiǎn)單,以至于可以毫不費(fèi)力的寫出一個(gè)能用的組合模式偽代碼2016-08-08
javaScript給元素添加多個(gè)class的簡(jiǎn)單實(shí)現(xiàn)
下面小編就為大家?guī)硪黄猨avaScript給元素添加多個(gè)class的簡(jiǎn)單實(shí)現(xiàn)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-07-07

