Home > WeChat Applet > Mini Program Development > Detailed explanation of the example codes for adding, deleting, modifying and checking operations in WeChat mini programs

Detailed explanation of the example codes for adding, deleting, modifying and checking operations in WeChat mini programs

高洛峰
Release: 2017-03-23 13:59:30
Original
7612 people have browsed it

This article mainly introduces the relevant information on the detailed operation examples of adding, deleting, modifying and checking the WeChat applet. Here is the example code. Friends in need can refer to it

WeChat applet Detailed explanation of adding, deleting, modifying and checking operation examples

1. Take the adding, deleting, modifying and checking of the delivery address as an example

2.File directory

Detailed explanation of the example codes for adding, deleting, modifying and checking operations in WeChat mini programs

  1. js file is a logical control, mainly it sends requests and receives data.

  2. json is used for local configuration of this page and covers the global app .json configuration,

  3. wxss is used for page style setting,

  4. wxml is the page, equivalent to html

Copy after login
Copy after login
收货人信息 姓名 电话 地址 送货时间 收货地址信息 <view> <view> <image></image> <view> <view><text>收货地址{{item.address}}</text><text>1km</text></view> <view>收货人{{item.name}}</view> <view>收货人电话{{item.mobile}}</view> <view>删除</view> <view>编辑</view> </view> </view> </view>

The front-end page mainly displays a form and existing consignee information

1. Several key points need to be understood

a.Form form needs to be bound to a submit Event, in the mini program, the attribute is bindsubmit,

bindsubmit=”formSubmit” The attribute value here, formSubmit, can be named to any value that conforms to the specification, which is equivalent to the # in the previous HTML ## onsubmit="formSubmit()", is a function name. The formSubmit function event is triggered when submitted. This function is written in js.

b. Other attributes are similar to the previous HTML. Note that the form must have name="value", and the back-end processing is the same as before. For example, name="username" PHP can use $_POST[ 'username'] to receive.

c. Since the mini program does not have an input submit button, there must be a submit button in each form,

a.Form表单,需要绑定一个submit事件,在小程序中,属性为bindsubmit,

bindsubmit=”formSubmit”   这里的属性值formSubmit,命名可以为符合规范的任意值,相当于以前html中的  onsubmit=”formSubmit()”,是一个函数名,当提交的时候触发formSubmit这个函数事件,这个函数写在js中。

b.其他的属性和之前的HTML差不多,注意的是,表单一定要有name=“value”,后端处理和以前一样,比如name=”username” PHP可以用 $_POST[‘username']来接收。

c.由于小程序没有input submit这个按钮,所以在每个form表单中都要有一个提交按钮,

,这个按钮就是用来开启提交事件的。

d.由于添加地址和编辑地址都是在一个页面的,所以我需要在每个表单中,加一个默认值变量,当点击修改的时候,默认值就显示在输入框中。

e.表单中有一个编辑,绑定了事件editClick,当点击这个按钮的时候,就会进入编辑模式

添加和修改的放在一个函数里面,但是修改数据的显示是另外一个函数

先说修改,点点击编辑的时候,触发editClick事件

JS如下:

editClick:function(event){

  var that = this;

  var id = event.currentTarget.dataset.editid;

  wx.request({

   url: 'https://shop.yunapply.com/home/shipping/edit?id='+id,

   data: {},

   method: 'GET',

   success: function(res){

    if(res.data.status == 0){

     wx.showToast({

      title: res.data.info,

      icon: 'loading',

      duration: 1500

     })

    }else{

     that.setData({

       "addressEdit": res.data.info,

     })

    }

   },

   fail:function(){

       wx.showToast({

        title: '服务器网络错误!',

        icon: 'loading',

        duration: 1500

       })

      }

  })

 },
Copy after login

为了更好理解,贴个图

 最下面有一个保存按钮,当点击编辑的时候,触发editClick:function(event),这个event是当前触发事件的对象,

var id = event.currentTarget.dataset.editid;  就是获取当前事件对象的dataset中的editid的值,这里id是当前地址的id

url: 'https://shop.com/home/shipping/edit?id='+id

Wx.request  的url,将id值放在url上,作为GET参数,传递到服务器。

data: {},是需要额外传递的数据

method: 'GET', 是数据传递方式  默认是“GET”,保持大写

data:{mobile:e.detail.value.mobile,password:e.detail.value.password},

这里的data就是POST给服务器端的数据 以{name:value}的形式传送

success:function()是请求状态成功触发是事件,也就是200的时候,注意,请求成功不是操作成功,请求只是这个程序到服务器端这条线的通的。

fail:function()就是网络请求不成功,触发的事件。

这里的一段代码是和PHP后端程序有关系的,具体流程是这样的,

1.GET通过数据到https://shop.com/home/Shipping/edit这个接口,用过THINKPHP的就会知道是HOME模块下的Shipping控制下的edit方法

2.后端PHP代码如下:

控制器 ShippingController.class.php

public function edit($id)
{
  $res = D('Shipping')->find($id);
  $this->success($res,'',true);
}
Copy after login

也就是说将这条数据取出来,没什么好说的。

 that.setData({

       "addressEdit": res.data.info,

     })

    }
Copy after login

这里请求成功以后,调用小程序 setData方法,将服务器端返回的信息放到addressEdit[]中,然后在前端页面调用{{addressEdit.id}}、{addressEdit.name}}、{addressEdit.mobile}}、{addressEdit.address}}将数据展示出来,这就是修改时候的操作。

接下来是提交表单的操作

Js代码如下

addSubmit:function(e){

  if(e.detail.value.mobile.length==0||e.detail.value.name.length==0 ||e.detail.value.address.length==0){

   wx.showToast({

    title: '收货人所有信息不得为空!',

    icon: 'loading',

    duration: 1500

   })

  }else if(e.detail.value.mobile.length != 11){

    wx.showToast({

    title: '请输入11位手机号码!',

    icon: 'loading',

    duration: 1500

   })

  }else{

   wx.request({ 

      url: 'https://shop.yunapply.com/home/shipping/save', 

      header: { 

       "Content-Type": "application/x-www-form-urlencoded" 

      },

      method: "POST",

      data:{id:e.detail.value.id,mobile:e.detail.value.mobile,name:e.detail.value.name,address:e.detail.value.address},

      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

         })

         setTimeout(function(){

          wx.navigateTo({

           url:'../address/index'

          })

         },1000)

       }

      },

      fail:function(){

       wx.showToast({

        title: '服务器网络错误!',

        icon: 'loading',

        duration: 1500

       })

      }  

     })

  }

 }
Copy after login

在前端的FORM表单中,当点击formtype=“submit”这个按钮的时候,触发addSubmit事件,前面的if都是JS验证,防止用户不填写信息。

1.其他的request请求差不多,找几个不一样的

url: 'https://shop.yunapply.com/home/shipping/save',
Copy after login

调用服务器端的save方法

  header: { 

    "Content-Type": "application/x-www-form-urlencoded" 

   },
Copy after login

由于POST和GET传送数据的方式不一样,POST的header必须是

"Content-Type": "application/x-www-form-urlencoded"
Copy after login

GET的header可以是 'Accept': 'application/json'

 data:{id:e.detail.value.id,mobile:e.detail.value.mobile,name:e.detail.value.name,address:e.detail.value.address},
Copy after login

这里是需要POST到服务器端的数据

Save方法代码

public function save()
{
  //$user_id
  $user_id = 2;
  if (IS_POST){
    $shipping = D('Shipping');
    if (!$shipping->create()){
      $this->error($shipping->getError(),'',true);
    }else{
      if (is_numeric($_POST['id'])){
        if ($shipping->editAddress($_POST['id'])){
          $this->success('地址修改成功','',true);
        }else{
          $this->error('地址修改失败','',true);
        }
      }else{
        if ($shipping->addAddress($user_id)){
          $this->success('添加地址成功','',true);
        }else{
          $this->error('添加地址失败','',true);
        }
      }
    }
  }
}
Copy after login

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

The above is the detailed content of Detailed explanation of the example codes for adding, deleting, modifying and checking operations in WeChat mini programs. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template