小程序织梦表单如何提交

首先我们再微信小程序内新建一个表单,加入事件

<form bindsubmit="formSubmit">


js内容

然后我们就用e.detail.value即可获取提交过来的数据!然后我们可以根据数据进行判断!

接着,我们就可以提交了!

重点织梦表单提交需要携带

diyid:'3',
do:'2',
dede_fields:'qudao,text;keshi,radio;name,text;tele,text;yytime,text;time,text;beizhu,multitext',
dede_fieldshash:'36d8ec8dc81cbdc59fb7a59f7c4c7ed3',

这4个东西,那么我们就直接写入data内!

然后提交!提交我们diy.php并不会返回json!

那么我们再diy.php内增加

$action = isset($action) && in_array($action, array('post', 'list', 'view','ajax')) ? $action : 'post';


然后if结束那里增加

else if($action == 'ajax')
{
    $state='0';//默认为失败
    if(empty($do))
    {
       $state='0';//默认为失败
    }
    elseif($do == 2)
    {
        $dede_fields = empty($dede_fields) ? '' : trim($dede_fields);
        $dede_fieldshash = empty($dede_fieldshash) ? '' : trim($dede_fieldshash);
        if(!empty($dede_fields))
        {
            if($dede_fieldshash != md5($dede_fields.$cfg_cookie_encode))
            {

                 $state='3';
            }
        }
        $diyform = $dsql->getOne("select * from dede_diyforms where diyid='$diyid' ");
        if(!is_array($diyform))
        {
             $state='2';
        }

        $addvar = $addvalue = '';

        if(!empty($dede_fields))
        {

            $fieldarr = explode(';', $dede_fields);
            if(is_array($fieldarr))
            {
                foreach($fieldarr as $field)
                {
                    if($field == '') continue;
                    $fieldinfo = explode(',', $field);
                    if($fieldinfo[1] == 'textdata')
                    {
                        ${$fieldinfo[0]} = FilterSearch(stripslashes(${$fieldinfo[0]}));
                        ${$fieldinfo[0]} = addslashes(${$fieldinfo[0]});
                    }
                    else
                    {
                        ${$fieldinfo[0]} = GetFieldValue(${$fieldinfo[0]}, $fieldinfo[1],0,'add','','diy', $fieldinfo[0]);
                    }
                    $addvar .= ', `'.$fieldinfo[0].'`';
                    $addvalue .= ", '".${$fieldinfo[0]}."'";
                }
            }
        

        }
     $query = "INSERT INTO `{$diy->table}` (`id`, `ifcheck` $addvar)  VALUES (NULL, 0 $addvalue); ";

   if($dsql->ExecuteNoneQuery($query)){
      $state='1';
      $data='成功';
      $result =array('state'=>$state,'data'=>$data);
      AjaxHead();
      // print_r($add);
      echo json_encode($result);//返回数据
}


    }
}

然后我们提交的后即可返回json,根据json即可判断是否提交成功


formSubmit: function(e) {
    var _this=this;
    let nowRequestTime = Date.parse(new Date())/ 1000;//获取当前时间
    var mytime =new Date().toLocaleString();
    console.log(mytime);
    var name=e.detail.value.name,
    tele=e.detail.value.tele,
    keshi=e.detail.value.keshi,
    yytime=e.detail.value.yytime,
    beizhu=e.detail.value.beizhu;
    if(name.length==0 || name=='' || name.length >=20 ){
      wx.showToast({
        title: '姓名不正确请重新输入',
        icon: 'none',
        duration: 1500
      });
   }
   else if(!(/^1[34578]\d{9}$/.test(tele))){
     wx.showToast({
        title: '电话不正确请重新输入',
        icon: 'none',
        duration: 1500
      });
   }
else{
        wx.request({
            url: 'https://www.csgt120.com/plus/diy.php',
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            contentType:'application/x-www-form-urlencoded; charset=utf-8',
            method: "POST",
            data: {
              action:'ajax',
              diyid:'3',
              do:'2',
              dede_fields:'qudao,text;keshi,radio;name,text;tele,text;yytime,text;time,text;beizhu,multitext',
              dede_fieldshash:'36d8ec8dc81cbdc59fb7a59f7c4c7ed3',
              time:nowRequestTime,
              qudao:'微信小程序',
              name:name,
              tele:tele,
              keshi:keshi,
              yytime:yytime,
              beizhu:beizhu
            },
            success: function(res) {
               wx.showToast({
                        title: "提交中",
                        icon: 'loading'
                    })
                console.log(res.data);
                  if (res.data.state=='1'){
                    wx.showToast({
                        title: "提交成功",
                        duration: 3500
                    });
                   _this.setData({//提交成功清空值
                      none:''
                    })

                } else {
                    wx.showToast({
                        title: "失败", //这里打印出登录成功
                        icon: 'success',
                        duration: 1000
                    })

                }

            }

        })
}
    },


写的好!

微信扫一扫赏大洋