微信小程序 PHP后端form表單提交實(shí)例詳解
微信小程序 PHP后端form表單
1.小程序相對(duì)于之前的WEB+PHP建站來說,個(gè)人理解為只是將web放到了微信端,用小程序固定的格式前前端進(jìn)行布局、事件觸發(fā)和數(shù)據(jù)的輸送和讀取,服務(wù)器端可以用任何后端語言寫,但是所有的數(shù)據(jù)都要以JSON的形式返回給小程序。
2.昨天寫了登錄注冊(cè)、忘記密碼功能,他們實(shí)質(zhì)上都是一個(gè)表單提交操作。因此就拿注冊(cè)功能來寫這個(gè)例子。
3.目錄圖
- js文件是邏輯控制,主要是它發(fā)送請(qǐng)求和接收數(shù)據(jù),
- json 用于此頁面局部 配置并且覆蓋全局app.json配置,
- wxss用于頁面的樣式設(shè)置,
- wxml就是頁面,相當(dāng)于html
4.樣式和json文件暫時(shí)不管了,我只是想回顧一下form表單的提交
5.Wxml文件代碼
<view class="load-head">
<image src="../../images/return.png" />
注冊(cè)
</view>
<view class="login">
<form bindsubmit="formSubmit">
<view class="field clearfix">
<label for="name"><image src="../../images/phone.png" /></label>
<input id="name" name="mobile" class="login-input" type="text" placeholder="請(qǐng)輸入手機(jī)號(hào)" />
</view>
<view class="field clearfix">
<label for="password"><image src="../../images/code.png" /></label>
<input id="password" class="login-input" type="password" placeholder="請(qǐng)輸入驗(yàn)證碼" />
<button class="get-code" hover-class="code-hover">獲取驗(yàn)證碼</button>
</view>
<view class="field clearfix">
<label for="password"><image src="../../images/password.png" /></label>
<input id="password" name="password" class="login-input" type="password" placeholder="請(qǐng)?jiān)O(shè)置6-20位登錄密碼" />
</view>
<view class="field clearfix">
<label for="repassword"><image src="../../images/password.png" /></label>
<input id="repassword" name="repassword" class="login-input" type="password" placeholder="請(qǐng)輸入確認(rèn)密碼" />
</view>
<button class="btn_login" formType="submit">注冊(cè)</button>
</form>
<view class="reg_forget clearfix">
<navigator url="../login/index" class="btn_reg">登錄</navigator>
<navigator url="../forget/index" class="btn_forget">忘記密碼</navigator>
</view >
</view>
6.其中幾個(gè)關(guān)鍵點(diǎn)需要理解
a.Form表單,需要綁定一個(gè)submit事件,在小程序中,屬性為bindsubmit,
bindsubmit=”formSubmit” 這里的屬性值formSubmit,命名可以為符合規(guī)范的任意值,相當(dāng)于以前html中的 onsubmit=”formSubmit()”,是一個(gè)函數(shù)名,當(dāng)提交的時(shí)候觸發(fā)formSubmit這個(gè)函數(shù)事件,這個(gè)函數(shù)寫在js中。
b.其他的屬性和之前的HTML差不多,注意的是,表單一定要有name=“value”,后端處理和以前一樣,比如name=”username” PHP可以用 $_POST[‘username']來接收。
C.由于小程序沒有input submit這個(gè)按鈕,所以在每個(gè)form表單中都要有一個(gè)提交按鈕,
<button formType="submit">注冊(cè)</button>,這個(gè)按鈕就是用來開啟提交事件的。
7.index.js代碼
Page({
data: {
},
formSubmit: function(e) {
if(e.detail.value.mobile.length==0||e.detail.value.password.length==0){
wx.showToast({
title: '手機(jī)號(hào)碼或密碼不得為空!',
icon: 'loading',
duration: 1500
})
setTimeout(function(){
wx.hideToast()
},2000)
}else if(e.detail.value.mobile.length != 11){
wx.showToast({
title: '請(qǐng)輸入11位手機(jī)號(hào)碼!',
icon: 'loading',
duration: 1500
})
setTimeout(function(){
wx.hideToast()
},2000)
}else if(e.detail.value.password.length <6 ||e.detail.value.password.length>20){
wx.showToast({
title: '請(qǐng)輸入6-20密碼!',
icon: 'loading',
duration: 1500
})
setTimeout(function(){
wx.hideToast()
},2000)
}else if(e.detail.value.password != e.detail.value.repassword){
wx.showToast({
title: '兩次密碼輸入不一致!',
icon: 'loading',
duration: 1500
})
setTimeout(function(){
wx.hideToast()
},2000)
}else{
wx.request({
url: 'https://shop.yunapply.com/home/Login/register',
header: {
"Content-Type": "application/x-www-form-urlencoded"
},
method: "POST",
data:{mobile:e.detail.value.mobile,password:e.detail.value.password},
success: function(res) {
if(res.data.status == 0){
wx.showToast({
title: res.data.info,
icon: 'loading',
duration: 1500
})
}else{
wx.showToast({
title: res.data.info,//這里打印出登錄成功
icon: 'success',
duration: 1000
})
}
}
})
}
},
})
8.需要注意的是
Page()這個(gè)方法是必須有的,里面放置js對(duì)象,用于頁面加載的時(shí)候,呈現(xiàn)效果
data: {},數(shù)據(jù)對(duì)象,設(shè)置頁面中的數(shù)據(jù),前端可以通過讀取這個(gè)對(duì)象里面的數(shù)據(jù)來顯示出來。
formSubmit: function 小程序中方法都是 方法名:function(),其中function可以傳入一個(gè)參數(shù),作為觸發(fā)當(dāng)前時(shí)間的對(duì)象
下面是函數(shù)的執(zhí)行,由于驗(yàn)證太多,我只拿一部分出來理解。
if(e.detail.value.mobile.length==0||e.detail.value.password.length==0){
wx.showToast({
title: '手機(jī)號(hào)碼或密碼不得為空!',
icon: 'loading',
duration: 1500
})
這里的e,就是當(dāng)前觸發(fā)事件的對(duì)象,類似于html onclick=“foo(this)”this對(duì)象,小程序封裝了許多內(nèi)置的調(diào)用方法,e.detail.value.mobile 就是當(dāng)前對(duì)象name=”mobile”的對(duì)象的值e.detail.value.mobile.length就是這個(gè)值的長度
showToast類似于JS中的alert,彈出框,title 是彈出框的顯示的信息,icon是彈出框的圖標(biāo)狀態(tài),目前只有l(wèi)oading 和success這兩個(gè)狀態(tài)。duration是彈出框在屏幕上顯示的時(shí)間。
9.重點(diǎn)來了
wx.request({
url: 'https://shop.com/home/Login/register',
header: {
"Content-Type": "application/x-www-form-urlencoded"
},
method: "POST",
data:{mobile:e.detail.value.mobile,password:e.detail.value.password},
success: function(res) {
if(res.data.status == 0){
wx.showToast({
title: res.data.info,
icon: 'loading',
duration: 1500
})
}else{
wx.showToast({
title: res.data.info,//這里打印出登錄成功
icon: 'success',
duration: 1000
})
}
},
fail:function(){
wx.showToast({
title: '服務(wù)器網(wǎng)絡(luò)錯(cuò)誤!',
icon: 'loading',
duration: 1500
})
}
})
wx.request({})是小程序發(fā)起https請(qǐng)求,注意http是不行的。
這里
a.url是你請(qǐng)求的網(wǎng)址,比如以前在前端,POST表單中action=‘index.php',這里的index.php是相對(duì)路徑,而小程序請(qǐng)求的網(wǎng)址必須是網(wǎng)絡(luò)絕對(duì)路徑。
比如:https://shop.com/home/Login/register
b.
header: {
"Content-Type": "application/x-www-form-urlencoded"
},
由于POST和GET傳送數(shù)據(jù)的方式不一樣,POST的header必須是
"Content-Type": "application/x-www-form-urlencoded"
GET的header可以是 'Accept': 'application/json'
c.一定要寫明method:“POST” 默認(rèn)是“GET”,保持大寫
data:{mobile:e.detail.value.mobile,password:e.detail.value.password},
這里的data就是POST給服務(wù)器端的數(shù)據(jù) 以{name:value}的形式傳送
d.成功回調(diào)函數(shù)
success: function(res) {
if(res.data.status == 0){
wx.showToast({
title: res.data.info,
icon: 'loading',
duration: 1500
})
}else{
wx.showToast({
title: res.data.info,
icon: 'success',
duration: 1000
})
}
}
e.success:function()是請(qǐng)求狀態(tài)成功觸發(fā)是事件,也就是200的時(shí)候,注意,請(qǐng)求成功不是操作成功,請(qǐng)求只是這個(gè)程序到服務(wù)器端這條線的通的。
fail:function()就是網(wǎng)絡(luò)請(qǐng)求不成功,觸發(fā)的事件。
f.
if(res.data.status == 0){
wx.showToast({
title: res.data.info,
icon: 'loading',
duration: 1500
})
}else{
wx.showToast({
title: res.data.info,//這里打印出登錄成功
icon: 'success',
duration: 1000
})
}
這里的一段代碼是和PHP后端程序有關(guān)系的,具體流程是這樣的,
1.POST通過數(shù)據(jù)到https://shop.com/home/Login/register這個(gè)接口,用過THINKPHP的就會(huì)知道是HOME模塊下的Login控制下的register方法
2.register方法根據(jù)POST過來的數(shù)據(jù),結(jié)合數(shù)據(jù)庫進(jìn)行二次驗(yàn)證,如果操作成功,返回什么,如果操作失敗,返回什么
3.后端PHP代碼如下:
控制器 LoginController.class.php
/**
* 用戶注冊(cè)
*/
public function register()
{
if (IS_POST) {
$User = D("User");
if (!$User->create($_POST, 4)) {
$this->error($User->getError(),'',true);
} else {
if ($User->register()){
$this->success('注冊(cè)成功!','',true);
}else{
$this->error('注冊(cè)失敗!','',true);
}
}
}
}
模型
UserModel.class.php 的register方法
public function register()
{
$mobile = I('post.mobile');
$password = I('post.password');
$res = D('User')->add(array(
'mobile'=> $mobile,
'password'=>md5($password),
'modifytime'=>date("Y-m-d H:i:s")
));
return $res;
}
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- 微信小程序開發(fā)之獲取用戶手機(jī)號(hào)碼(php接口解密)
- 微信小程序發(fā)送訂閱消息的方法(php 為例)
- 基于PHP實(shí)現(xiàn)微信小程序客服消息功能
- 微信小程序?qū)W習(xí)筆記之表單提交與PHP后臺(tái)數(shù)據(jù)交互處理圖文詳解
- 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:微信小程序 微信支付服務(wù)端集成實(shí)例詳解及源碼下載
- PHP小程序后臺(tái)部署運(yùn)行 LNMP+WNMP的方法
相關(guān)文章
Three.js實(shí)現(xiàn)雪糕地球的使用示例詳解
這篇文章主要為大家介紹了Three.js實(shí)現(xiàn)雪糕地球的使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
微信小程序 實(shí)現(xiàn)點(diǎn)擊添加移除class
這篇文章主要介紹了 微信小程序 實(shí)現(xiàn)點(diǎn)擊添加移除class的相關(guān)資料,需要的朋友可以參考下2017-06-06
uniapp自定義相機(jī)實(shí)現(xiàn)示例詳解
這篇文章主要為大家介紹了uniapp自定義相機(jī)實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
微信小程序 Windows2008 R2服務(wù)器配置TLS1.2方法
微信小程序免費(fèi)SSL證書https、TLS版本問題的解決方案《二十四》request:fail錯(cuò)誤(含https解決方案)(真機(jī)預(yù)覽問題把下面的代碼復(fù)制到PowerShell里運(yùn)行一下,然后重啟服務(wù)器。# Enables TLS 1.2 on ...,需要的朋友可以參考下2016-12-12

