最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

微信小程序?qū)W習(xí)筆記之表單提交與PHP后臺數(shù)據(jù)交互處理圖文詳解

 更新時(shí)間:2019年03月28日 11:50:56   作者:李維山  
這篇文章主要介紹了微信小程序?qū)W習(xí)筆記之表單提交與PHP后臺數(shù)據(jù)交互處理,結(jié)合實(shí)例形式詳細(xì)分析了微信小程序前臺數(shù)據(jù)form表單提交及后臺使用php進(jìn)行處理相關(guān)操作技巧,并配以圖文形式詳細(xì)說明,需要的朋友可以參考下

本文實(shí)例講述了微信小程序?qū)W習(xí)筆記之表單提交與PHP后臺數(shù)據(jù)交互處理。分享給大家供大家參考,具體如下:

前面一篇結(jié)介紹了微信小程序函數(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后臺服務(wù)器】

使用 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內(nèi)容如下,這些就是PHP后臺獲得的數(shù)據(jù),可以對其進(jìn)行數(shù)據(jù)庫操作:

array (
 'nickname' => '李棟',
 'password' => '123456',
 'sex' => '男',
 'status' => 'true',
 'aihao' => 'cy,hj,tt',
 'grade' => '66',
)

【PHP后臺對提交過來的數(shù)據(jù)進(jìn)行判斷、處理,返回狀態(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();返回?cái)?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ā)有所幫助。

相關(guān)文章

最新評論

金沙县| 中方县| 永和县| 塔城市| 宝兴县| 博客| 芦山县| 行唐县| 遂平县| 雅江县| 佳木斯市| 台前县| 新津县| 封开县| 陇川县| 巩留县| 宝丰县| 保康县| 利川市| 平顺县| 巍山| 桦甸市| 永嘉县| 玛多县| 凤翔县| 威宁| 江城| 太湖县| 克拉玛依市| 桦南县| 叶城县| 乳山市| 苏尼特右旗| 房山区| 西充县| 搜索| 湟源县| 吉安县| 湛江市| 开鲁县| 峨眉山市|