request请求后台接口php简单实例

form.js

// pages/form/form.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    usfocus: false,
    phfocus: false,
    titfocus: false,
    adfocus: false,
    confocus: false
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  formSubmit(e) {
    let photo = /^(((13[0-9]{1})|(14[0-9]{1})|(15[0-9]{1})|(17[0-9]{1})|(18[0-9]{1})|(199))+\d{8})$/; //验证130-139,150-159,180-189号码段的手机号码
    let params = e.detail.value;
    console.log(params);
    if (params.username == '') {
      showToast("用户名");
      this.setData({
        usfocus: true
      })
      return;
    }
    if (params.phone == '') {
      showToast("手机号");
      this.setData({
        phfocus: true
      })
      return;
    }
    if (!photo.test(params.phone)) {
      wx.showToast({
        title: '手机号格式不正确!',
        duration: 2000,
        icon: "none"
      })
      this.setData({
        phfocus: true
      })
      return;
    }
    // if (params.title == '') {
    //   showToast("咨询课程");
    //   this.setData({
    //     titfocus: true
    //   })
    //   return;
    // }
    // if (params.ad == '') {
    //   showToast("就近校区");
    //   this.setData({
    //     adfocus: true
    //   })
    //   return;
    // }
    // if (params.content == '') {
    //   showToast("备注");
    //   this.setData({
    //     confocus: true
    //   })
    //   return;
    // }
    function showToast(val) {
      wx.showToast({
        title: val + '不能为空',
        duration: 2000,
        icon: "none"
      })
    }
    // "enterprise_id=630&project=junqiao信息流&admin_name=zdadmin&username=" + params.username + "&phone=" + params.phone + "&ad=" + params.ad + "&title=" + params.title + "&content=" + params.content + ""
    wx.request({
      url: 'http://fncms.xxxx.cn/js_guestbook_wx.php', //仅为示例,并非真实的接口地址
      method: "POST",
      data: {
        enterprise_id: '630',
        project: '官网小程序',
        admin_name: 'zdadmin',
        username: params.username,
        phone: params.phone,
        ad: params.ad,
        title: params.title,
        content: params.content
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success: function (res) {
        console.log(res.data)
        if (res.data==1) {
          wx.showToast({
            title: '提交成功',
            icon: 'success',
            duration: 2000
          })
        } else if (res.data == 9) {
          wx.showToast({
            title: '失败:你的手机号已经申请过了!',
            icon: 'none',
            duration: 2000
          })
        } else {
          wx.showToast({
            title: '提交失败',
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  }
})

 url: 'http://fncms.xxxx.cn/js_guestbook_wx.php'

<?php  
include("config.php");
//实现跨域,关键在于服务器,客户端的代码按照正常的方式编写即可。对于服务器,只需要在收到OPTIONS请求的地方,返回的头信息中增加该属性即可
header("Access-Control-Allow-Origin:*"); //*号表示所有域名都可以访问  
header("Access-Control-Allow-Method:POST,GET");  

header("Content-type: text/html;charset=utf-8");
//过虑回车键,空格键之类

function unhtml($str) 
{ 
$str = strip_tags($str,"");
$str = ereg_replace("\t","",$str);
$str = ereg_replace("\r\n","",$str);
$str = ereg_replace("\r","",$str);
$str = ereg_replace("\n","",$str);
$str = ereg_replace(" "," ",$str); 
$str = ereg_replace("VALUES","[***]",$str); 
$str = ereg_replace("values","[***]",$str); 
$str = ereg_replace("INTO","[***]",$str); 
$str = ereg_replace("into","[***]",$str); 
$str = ereg_replace("DELETE","[***]",$str); 
$str = ereg_replace("delete","[***]",$str); 
$str = ereg_replace("FROM","[***]",$str); 
$str = ereg_replace("from","[***]",$str); 
$str = ereg_replace("INSERT","[***]",$str); 
$str = ereg_replace("insert","[***]",$str); 
$str = str_replace(array("i", "I", "d", "D", "O", "o", "u", "U", "V", "v", "=", "?", "&", "q", "Q", "("), "[*]", $str); 
return $str; 
} 
function getIP() { 
if (getenv('HTTP_CLIENT_IP')) { 
$ip = getenv('HTTP_CLIENT_IP'); 
} 
elseif (getenv('HTTP_X_FORWARDED_FOR')) { 
$ip = getenv('HTTP_X_FORWARDED_FOR'); 
} 
elseif (getenv('HTTP_X_FORWARDED')) { 
$ip = getenv('HTTP_X_FORWARDED'); 
} 
elseif (getenv('HTTP_FORWARDED_FOR')) { 
$ip = getenv('HTTP_FORWARDED_FOR'); 

} 
elseif (getenv('HTTP_FORWARDED')) { 
$ip = getenv('HTTP_FORWARDED'); 
} 
else { 
$ip = $_SERVER['REMOTE_ADDR']; 
} 
return $ip; 
} 

//判断电话号码
if(preg_match("/^1[34578]{1}\d{9}$/",$_POST['phone'])){  //是手机号码
	// 预处理及绑定
	$enterprise_id = trim($_POST['enterprise_id']);
	
	//判定企业id是否正确
	$enterprise_m_id=$enterprise_id;
	$sql_d9 = "Select id FROM data_enterprise where id=".$enterprise_m_id;
	$result_d9 = mysqli_query($conn, $sql_d9);  
	$rs_d9 = mysqli_fetch_assoc($result_d9);  
	$rs_d9_id=$rs_d9['id'];	
	
	
	if($enterprise_id=="" || is_numeric($enterprise_id)==false || $rs_d9_id==""){//没有填写企业ID
	      $datausername="data_guestbook";		
	}else{
	      $datausername=$enterprise_id."_data_guestbook";	
	}
	

	$stmt = $conn->prepare("INSERT INTO ".$datausername." (project, ad, type1, type2, type3, type4, type5, title, username, content, url, qq, phone, wx, email, admin_name,enterprise_id,ip,checkbox1,checkbox2,checkbox3,checkbox4,checkbox5,c1,c2,c3,c4,c5) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?,?,?,?, ?,?,?,?,?, ?,?,?,?,?)");
	$stmt->bind_param("ssssssssssssssssssssssssssss", $project, $ad, $type1, $type2, $type3, $type4, $type5, $title, $username, $content, $url, $qq, $phone, $wx, $email, $admin_name,$enterprise_id,$ip,$checkbox1,$checkbox2,$checkbox3,$checkbox4,$checkbox5,$c1,$c2,$c3,$c4,$c5);
	 
	// 设置参数并执行

	$project = trim($_POST['project']);
	$ad = $_POST['ad']=="undefined"?"":trim($_POST['ad']);
	$type1 =$_POST['type1']=="undefined"?"":trim($_POST['type1']);
	
	$type2 = $_POST['type2']=="undefined"?"":trim($_POST['type2']);
	$type3 = $_POST['type3']=="undefined"?"":trim($_POST['type3']);
	$type4 = $_POST['type4']=="undefined"?"":trim($_POST['type4']);
	$type5 = $_POST['type5']=="undefined"?"":trim($_POST['type5']);
	
	$checkbox1 = $_POST['checkbox1']=="undefined"?"":trim($_POST['checkbox1']);
	$checkbox2 = $_POST['checkbox2']=="undefined"?"":trim($_POST['checkbox2']);
	$checkbox3 = $_POST['checkbox3']=="undefined"?"":trim($_POST['checkbox3']);
	$checkbox4 = $_POST['checkbox4']=="undefined"?"":trim($_POST['checkbox4']);
	$checkbox5 = $_POST['checkbox5']=="undefined"?"":trim($_POST['checkbox5']);
	
    $c1 = $_POST['c1']=="undefined"?"":trim($_POST['c1']);
	$c2 = $_POST['c2']=="undefined"?"":trim($_POST['c2']);
	$c3 = $_POST['c3']=="undefined"?"":trim($_POST['c3']);
	$c4 = $_POST['c4']=="undefined"?"":trim($_POST['c4']);
	$c5 = $_POST['c5']=="undefined"?"":trim($_POST['c5']);
	
	$title = $_POST['title']=="undefined"?"":trim($_POST['title']);
	
	$username = $_POST['username']=="undefined"?"":trim($_POST['username']);
	$content = $_POST['content']=="undefined"?"":trim($_POST['content']);
	$url = $_POST['url']=="undefined"?"":trim($_POST['url']);
	
	$qq = $_POST['qq']=="undefined"?"":trim($_POST['qq']);
	$phone = trim($_POST['phone']);
	$wx = $_POST['wx']=="undefined"?"":trim($_POST['wx']);
	
	$email = $_POST['email']=="undefined"?"":trim($_POST['email']);
	$admin_name = trim($_POST['admin_name']);
	
	$enterprise_id = trim($_POST['enterprise_id']);
	$ip=getIP();
	//$ip=$_SERVER["REMOTE_ADDR"]; 
	//$ip = "127.0.0.1"; 
	
	//判断手机号码是否已经存在
	$sql_dt = "Select id,phone,time FROM ".$datausername." where phone=".$phone;
	$result_dt = mysqli_query($conn, $sql_dt);
	$v_result = mysqli_fetch_assoc($result_dt); 
	if (mysqli_num_rows($result_dt) > 0) {	//已经存在电话
		//判断时间大于7天后就能重新注册
		//$msg=9; 
		//echo $msg;
		
		 
		 $zero1=date("Y-m-d");  
		 $zero2=$starttime=date("Y-m-d", strtotime("+7 days", strtotime($v_result['time'])));//+7;从现在时间将来7  
		 if(strtotime($zero1)<strtotime($zero2)){//不够7天,不能申请
				$msg=9; 
				echo $msg;
		      //echo "zero1早于zero2";  
		 }else{
			   $stmt->execute();
				$msg=1; 
				echo $msg;
		      //echo "zero2早于zero1";  
		 } 
	} else {//不存在记录,就添加
	    $stmt->execute();
		$msg=1; 
		echo $msg;
	}
	
	
	$stmt->close();
	$conn->close();
}else{ //不是手机号码
    $msg=2;  
	 echo $msg;
}

//输出结果
//$msg=1; 
//$msg='电话:'.$_POST['phone'].',project:'.$_POST['project'].',ad:'.$_POST['ad'].',type1:'.$_POST['type1'].',type2:'.$_POST['type2'].',type3:'.$_POST['type3'].',title:'.$_POST['title'].',username:'.$_POST['username'].',content:'.$_POST['content'].',url:'.$_POST['url'].',qq:'.$_POST['qq'].',wx:'.$_POST['wx'].',email:'.$_POST['email'].',admin_name:'.$_POST['admin_name'];
//echo $msg;
  
?>  

或者直接输出值

         $msg=$_POST['phone'];  
	 echo $msg;


form.wxml

<view class='my-form'>
  <form bindsubmit="formSubmit" bindreset="formReset">
    <!-- <view class='radio-group'>
      <view class='ypzw'>应聘职位:</view>
      <radio-group>
        <view class='radio-item'>
          <radio value='教务班主任' id='jwbzr' name="zw" checked></radio>
          <label for="jwbzr">教务班主任</label>
          <radio value='全职讲师' id='qzjs' name="zw"></radio>
          <label for="qzjs">全职讲师</label>
        </view>
        <view class='radio-item'>
          <radio value='前台课程顾问' id='qtkcgw' name="zw"></radio>
          <label for="qtkcgw">前台课程顾问</label>
          <radio value='课程顾问' id='kcgw' name="zw"></radio>
          <label for="kcgw">课程顾问</label>
        </view>
      </radio-group>
    </view> -->
    <view class='input-item'>
      <view class='input-title'>
        <label for='username'>姓名:</label>
      </view>
      <view class='input-input'>
        <input type='text' placeholder='请填写姓名' id='username' name="username" focus="{{usfocus}}"></input>
      </view>
    </view>
    <view class='input-item'>
      <view class='input-title'>
        <label for='username'>手机:</label>
      </view>
      <view class='input-input'>
        <input type='text' placeholder='请填写您的手机' id='phone' name="phone" focus="{{phfocus}}"></input>
      </view>
    </view>
    <view class='input-item'>
      <view class='input-title'>
        <label for='username'>想了解的课程:</label>
      </view>
      <view class='input-input'>
        <input type='text' placeholder='想了解的课程' id='title' name="title" focus="{{titfocus}}"></input>
      </view>
    </view>
    <view class='input-item'>
      <view class='input-title'>
        <label for='username'>就近校区:</label>
      </view>
      <view class='input-input'>
        <input type='text' placeholder='就近校区' id='ad' name="ad" focus="{{adfocus}}"></input>
      </view>
    </view>
    <view class='input-item'>
      <view class='input-title'>
        <label for='username'>备注:</label>
      </view>
      <view class='input-input'>
        <textarea placeholder='备注' id='content' name="content" focus="{{confocus}}"></textarea>
      </view>
    </view>
    <view class="btn-area">
      <button formType="submit" type='primary' size='mini'>提交</button>
      <button formType="reset" type='warn' size='mini'>重置</button>
    </view>
  </form>
</view>
<view wx:if="{{isSubmit}}">  
  {{warn ? warn : "是否公开信息:"+username+",手机号:"+phone}}  
</view>  

form.wxss

/* pages/form/form.wxss */

.my-form {
  padding: 20rpx;
}

.ypzw {
  font-weight: bold;
  margin-bottom: 20rpx;
}

.radio-group {
  font-size: 14px;
  margin-bottom: 20rpx;
}

.radio-group radio {
  transform: scale(0.8);
}

.radio-group label {
  margin-right: 20rpx;
}

.radio-item {
  margin-bottom: 10rpx;
}

.input-item {
  border-bottom: 1px solid #ebebeb;
}

.input-title {
  margin-right: 15rpx;
  font-size: 14px;
  font-weight: bold;
  
  padding: 20rpx;
}
.input-input {
  font-size: 14px;
}

.input-input input {
  height: 80rpx;
  padding-left: 20rpx;
  background: #ebebeb;
}
.input-input textarea{
  height: 200rpx;
  background: #ebebeb;
  width: 100%;
  box-sizing: border-box;
  padding: 20rpx;
}
.btn-area{
  margin-top: 30rpx;
}
.btn-area button{
  margin-right: 15rpx;
}

form.json

{
  "navigationBarTextStyle": "#fff",
  "navigationBarTitleText": "在线应聘",
  "backgroundColor": "#eeeeee",
  "backgroundTextStyle": "light"
}

猜你喜欢

转载自blog.csdn.net/haibo0668/article/details/80582834
今日推荐