微信小程序留言功能前+后端php实现

实现功能:小程序客户端输入留言内容,这些内容会发到数据库表中,然后再从数据库返回到前端加载出来。

思路:总体上,是用

bindsubmit=“formSubmit”>把输入的留言内容以及授权时获取的用户姓名、头像地址、性别等信息上传到服务器,然后后端服务器wx:for循环渲染数据库表中的信息。

  • 先附上客户端页面的内容
    微信小程序留言功能前+后端php实现_第1张图片
    初次使用,要先授权登录
    微信小程序留言功能前+后端php实现_第2张图片

然后输入留言的内容:
微信小程序留言功能前+后端php实现_第3张图片

下拉刷新,就可以加载出留言的内容了:(我留了好多条)
微信小程序留言功能前+后端php实现_第4张图片

功能大致如上,下面附上代码
注意,“失物认领所”那部分是在index里(加载留言的页面)
"留言处"是在logs里面(输入留言内容)

index.html


失物认领所



{{item.result}}

以下是失物信息




	

{{item.nickname}} 
{{item.liuyantext}}

{{item.lytime}}


index.js

//index.js
const app = getApp()
Page({
  /**
  * 页面的初始数据
  */
  data: {
    tupian: '',
    uploadpath:''
  },
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading();
    var that = this
    wx.request({
      url: '填写加载出内容的url地址,我这里用的后端是PHP的',
      method: "POST",
      header: {
        'Content-Type': 'application/x-www-form-urlencoded',
        /*'content-type': 'application/x-www-form-urlencoded;charset=utf-8'*/
      },
      success: function (res) {
        //将获取到的json数据,存在名字叫list的这个数组中
        that.setData({
          liuyanlist: res.data,
          //res代表success函数的事件对,data是固定的,liuyanlist是数组
        })
      }
    })
    // 隐藏导航栏加载框
    wx.hideNavigationBarLoading();
    // 停止下拉动作
    wx.stopPullDownRefresh();
  },
  //加载最新数据
  onLoad: function () {
    var that = this
    wx.request({
      url: '填写加载出内容的url地址,我这里用的后端是PHP的',
      method: "POST",
      header: {
        'Content-Type': 'application/x-www-form-urlencoded',
      },
      success: function (res) {
        //将获取到的json数据,存在名字叫list的这个数组中
        that.setData({
          liuyanlist: res.data,
          //res代表success函数的事件对,data是固定的,liuyanlist是数组
        })
      }
    })
  },

  onShareAppMessage: function () {   
    var sharetitle,
      tid = this.data.tid,
      m = this.data.m,
      flag = this.data.lastflag,
      title = this.data.sharetitle;
    return {
      title: flag == true ? title : '分享~',
      desc: '你好吗?(这里是分享页面的函数内容)',
      path: '/pages/index/index?tid=' + tid + '&m=' + m
    }
  }
})

WXSS

.ttext{
  text-align:center;
  color:rgba(9, 255, 0, 0.349);
  font-size:200%;
  text-decoration:blink;
  text-shadow: -20 -20 -20 blue;
  /*background-color:rgb(133, 130, 130);*/
  font-style:oblique;
  margin-top:20px;
}
.input-style{
height:100px;
width:100%;
/*margin:10px auto;*/
margin-top:9px;
margin-left:8px;
margin-right:8px;
color:darkgray;
font-size:110%;
}
.anniu{
  height:40px;
  width:40px;
  float:left;
  margin-left:20px;
  margin-top:10px;
  margin-bottom:5px;
}
.text{
  float:left;
  position:relative;
  color:#999;
  font-size:80%;
}
.show-image{
width:100px;
height:50px;
margin-bottom:8px;
}
.shurukuan1{
  color:rgb(204, 201, 201);
}
.btn{
width: 88%;
margin:5px auto;
}
.result{
text-align: center;
font-size: 14px;
color: #f00;
margin-top: 20px;
}
.liuyanview{
/*width:100%;*/
/*margin: 10px auto;*/
margin-right: 10px;
margin-bottom: 10px;
margin-top: 10px;
/*margin-top:-40px;*/
}
.headimg{
width: 35px;
height: 35px;
border-radius: 100%;
float:left;
}
.headimg image{
width: 35px;
height: 35px;
border-radius: 100%;
float:left;
}
.nickname_liuyantext{
/*width: calc(100% - 55px);*/
width:100%;
float: right;
margin-top:-120px;
}
.nickname_liuyantext .nickname{
font-size: 15px;
color: rgb(128, 171, 236);
margin-left: 45px;
margin-top:80px
}
.nickname_liuyantext .text{
font-size: 17px;
color: #666;
margin-left:45px;
margin-top: 6px;
margin-bottom:7px;
}
.nickname_liuyantext .time{
font-size: 11px;
color: #999;
/*margin-top: 5px;*/
margin-left: 45px;
margin-top: 6px;
float:left;
margin-bottom:10px;
}
.nickname_liuyantext.tupian{
  height:50%;
}
.tupian{
  margin-bottom: 20px;
}
.tianjiatupian{
  color:#999;
  font-size:80%;
}

index.json:

{
 "enablePullDownRefresh":true,
 "backgroundTextStyle": "dark"
}

以上是加载留言的代码,url要根据自己的来写,只要输入正确的url,就能实现加载留言内容了,当然需要你的数据库表里有内容。
下面是“留言处”的代码

logs.html