微信小程序前端傳數據給后端
現在已經連接到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>