微信小程序前端传数据给后端
现在已经连接到php文件了
但是php操纵的数据库和网页没有反应
先粘一下代码:
wxml:
<!--pages/sql/sql.wxml-->
<form bindsubmit="formSubmit" bindreset="formReset">
<view class="section">
<view class="section__title">姓名</view>
<input name="xingming" placeholder="请输入姓名" />
</view>
<view class="section section_gap">
<view class="section__title">性别</view>
<radio-group name="xingbie">
<label><radio value="男"/>男</label>
<label><radio value="女"/>女</label>
</radio-group>
</view>
<view class="section section_gap">
<view class="section__title">爱好</view>
<checkbox-group name="aihao">
<label><checkbox value="旅游"/>旅游</label>
<label><checkbox value="看书"/>看书</label>
<label><checkbox value="电动"/>电动</label>
<label><checkbox value="篮球"/>篮球</label>
</checkbox-group>
</view>
<view class="btn-area">
<button formType="submit">提交</button>
<button formType="reset">重置</button>
</view>
</form>
js:
const app = getApp()
Page({
data: {
},
formSubmit: function (e) {
//console.log(e.detail.value);
if (e.detail.value.xingming.length == 0 || e.detail.value.xingming.length >= 8) {
wx.showToast({
title: '姓名不能为空或过长!',
icon: 'loading',
duration: 1500
})
setTimeout(function () {
wx.hideToast()
}, 2000)
} else if (e.detail.value.xingbie.length == 0) {
wx.showToast({
title: '性别不能为空!',
icon: 'loading',
duration: 1500
})
setTimeout(function () {
wx.hideToast()
}, 2000)
} else if (e.detail.value.aihao.length == 0) {
wx.showToast({
title: '爱好不能为空!',
icon: 'loading',
duration: 1500
})
setTimeout(function () {
wx.hideToast()
}, 2000)
} else {
wx.request({
url: 'http://localhost/wx.php',
header: {
"Content-Type": "application/x-www-form-urlencoded"
},
method: "POST",
data: { xingming: e.detail.value.xingming, xingbie: e.detail.value.xingbie, aihao: e.detail.value.aihao },
success: function (res) {
console.log(res.data);
if (res.data.status == 0) {
wx.showToast({
title: '提交失败!!!',
icon: 'loading',
duration: 1500
})
} else {
wx.showToast({
title: '提交成功!!!',//这里打印出登录成功
icon: 'success',
duration: 1000
})
}
}
})
}
},
})
php:
<?php
$con=mysqli_connect("localhost","root","","phpcrud");
if (!$con)
{
die('Could not connect: ' . mysqli_connect_error());
}
mysqli_query($con,"set names utf8");
if (!empty($_POST['xingming'])&&!empty($_POST['xingbie'])&&!empty($_POST['aihao'])){
$sql="INSERT INTO wedb (xingming, xingbie, aihao) VALUES ('$_POST[xingming]','$_POST[xingbie]','$_POST[aihao]')";
$result = mysqli_query($con,$sql);
if (!$result)
{
die('我他妈的,操 ' . mysqli_connect_error());
}
}
$sql1 = "SELECT * FROM wedb";
$result1 = mysqli_query($con,$sql1);
?>
<!doctype html>
<html lang="zh-cn">
<head>
<meta charset="utf-8">
<title>表单</title>
</head>
<body style="margin:50px;">
<script language="JavaScript">
function myrefresh()
{
window.location.reload();
}
setTimeout('myrefresh()',10000); //指定1秒刷新一次
</script>
<table style='text-align:left;' border='1'>
<tr><th>id</th><th>名字</th><th>性别</th><th>爱好</th></tr>
<?php
while ($bookInfo = mysqli_fetch_array($result1,MYSQLI_ASSOC)){ //返回查询结果到数组
$xingming = $bookInfo["xingming"]; //将数据从数组取出
$xingbie = $bookInfo["xingbie"];
$aihao = $bookInfo["aihao"];
$id = $bookInfo["id"];
echo "<tr><td>{$id}</td><td>{$xingming}</td><td>{$xingbie}</td><td>{$aihao}</td></tr>";
}
//释放结果集
mysqli_free_result($result1);
mysqli_close($con);
?>
</table>
</body>
</html>