百度小程序post提交from表单,发起端+接收端
- 共 3,310 次检阅

发起端 swan

  <form bindsubmit="formSubmit">
    <view class="field clearfix">
      <input id="canshu1" value="1" name="canshu1" class="login-input" type="text" placeholder="请输入您的学历" />
      <input id="canshu2" value="2" name="canshu2" class="login-input" type="text" placeholder="请输入您的工作年限" />
      <input id="canshu3" value="3"  name="canshu3" class="login-input" type="text" placeholder="请输入您的姓名" />
      <input id="canshu4" value="4"  name="canshu4" class="login-input" type="text" placeholder="请输入您的手机号码" />
      <input id="canshu5" value="5"  name="canshu5" class="login-input" type="text" placeholder="请输入您目前在职的公司" />
      <input id="canshu6" value="6"  name="canshu6" class="login-input" type="text" placeholder="请输入您的职位" />
      <input  name="canshu7" class="login-input" value="百度小程序-{{content.title}}"/ style="display:none;">
    </view>
    <button class="btn_login" formType="submit">提交</button>

发起端js

  formSubmit: function(e) {
    // 判断参数1和2是否为空,
    if(e.detail.value.canshu1.length==0||e.detail.value.canshu2.length==0){
     swan.showToast({
      title: '填写参数12!',
      icon: 'loading',
      duration: 1500
     })
     setTimeout(function(){
       swan.hideToast()
      },2000)
    // 判断参数3和4是否为空,
    }else if(e.detail.value.canshu3.length==0||e.detail.value.canshu4.length==0){
      swan.showToast({
      title: '填写参数34!',
      icon: 'loading',
      duration: 1500
     })
     setTimeout(function(){
       swan.hideToast()
      },2000)
    }else{
     swan.request({
        url: 'https://服务端接收地址',
        header: {
            "Content-Type": "application/json"
        },
       // 提交方式
        method: "POST",
        data:{
            canshu1:e.detail.value.canshu1,
            canshu2:e.detail.value.canshu2,
            canshu3:e.detail.value.canshu3,
            canshu4:e.detail.value.canshu4,
            canshu5:e.detail.value.canshu5,
            canshu6:e.detail.value.canshu6,
            canshu7:e.detail.value.canshu7
        },
        success: function(res) {
        console.log(res.data);
         if(res.data.status == 0){
            swan.showToast({
            title: '提交成功!',
            icon: 'loading',
            duration: 1500
           })
         }else{
           swan.showToast({
            title: '提交失败!',
            icon: 'success',
            duration: 1000
           })
         }
        }
       })
    }
   },

TP5 接收端

$response = json_decode(file_get_contents('php://input'), true);
            $datafrom = [
                'canshu3'=>$response['canshu3'],
                'canshu4'=>$response['canshu4'],
                'canshu1'=>$response['canshu1'],
                'canshu5'=>$response['canshu5'],
                'canshu6'=>$response['canshu6'],
                'canshu2'=>$response['canshu2'],
                'canshu7'=>$response['canshu7']
            ];
             // 返回提交状态(未写判断)
             return json(['status' => '0']);

 

分享到:

这篇文章还没有评论

发表评论