微信小程序之表單提交與PHP后臺(tái)數(shù)據(jù)交互處理的示例分析

這篇文章給大家分享的是有關(guān)微信小程序之表單提交與PHP后臺(tái)數(shù)據(jù)交互處理的示例分析的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。

創(chuàng)新互聯(lián)公司自2013年創(chuàng)立以來(lái),是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目做網(wǎng)站、網(wǎng)站建設(shè)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元托克遜做網(wǎng)站,已為上家服務(wù),為托克遜各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:028-86922220

具體如下:

【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:

微信小程序之表單提交與PHP后臺(tái)數(shù)據(jù)交互處理的示例分析

重置觸發(fā)formReset:

微信小程序之表單提交與PHP后臺(tái)數(shù)據(jù)交互處理的示例分析


【表單數(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ù)庫(kù)操作:

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

微信小程序之表單提交與PHP后臺(tái)數(shù)據(jù)交互處理的示例分析

【PHP后臺(tái)對(duì)提交過(guò)來(lái)的數(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
     });
    }
   }
  })
 }
})

微信小程序之表單提交與PHP后臺(tái)數(shù)據(jù)交互處理的示例分析

【請(qǐng)求PHP后臺(tái)API接口,獲得數(shù)據(jù),渲染頁(yè)面】

示例如下,獲得10條博客信息顯示在頁(yè)面中(接口用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>

頁(yè)面加載,顯示如下:

微信小程序之表單提交與PHP后臺(tái)數(shù)據(jù)交互處理的示例分析

感謝各位的閱讀!關(guān)于“微信小程序之表單提交與PHP后臺(tái)數(shù)據(jù)交互處理的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

當(dāng)前標(biāo)題:微信小程序之表單提交與PHP后臺(tái)數(shù)據(jù)交互處理的示例分析
網(wǎng)頁(yè)路徑:http://bm7419.com/article14/pcijge.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、標(biāo)簽優(yōu)化、自適應(yīng)網(wǎng)站、網(wǎng)站收錄、面包屑導(dǎo)航、網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站