JS获取form表单提交的数据(ajax获取表单提交数据进行请求)
开发说明
这次用原生的php和js开发增删改查的功能,数据是用php遍历的,数据修改有用form表单和ajax去请求,一个页面要修改和删除的请求(数据是遍历的)!修改是用表单提交的,重置密码和删除是用jq获取表单提交的内容,在用ajax提交到php处理数据
代码
//php遍历数据
<body>
<div style="margin:0 auto;width:100%">
<div class="col-md-6" style="margin:0 auto;width:100%">
<table class="table table-hover table-striped" style="margin:0 auto;width:800px" >
<tr >
<td colspan="6"><h6><a href="index.php">回首页</a></h6><h1 class="text-center">用户管理</h1></td>
</tr>
<tr>
<td colspan="6"><h4><a href="add.php">新增用户</a></h4></td>
</tr>
<tr >
<td>id</td>
<td>用户名</td>
<td>注册时间</td>
<td>过期时间</td>
<td>操作</td>
</tr>
<?php
foreach($userlist as $list){
echo '<form class="form-inline" action="update.php" method="post" >';
echo "<input type='text' style='display:none;' name='id' value=".$list['id'].">";
echo ' <tr><td>'.$list['id'].'</td>';
echo '<td>'.$list['username'].'</td>';
echo '<td>'.date("Y-m-d",$list['createtime']).'</td>';
echo '<td>'.date("Y-m-d",$list['createtime']+$list['indate']*86400).'</td>';
echo '<td><button type="submit" class="btn btn-info">修改</button></form>';
echo '<form class="form-inline" action="" οnclick="return deleteFn(this)">';
echo "<input type='text' style='display:none;' name='id' value=".$list['id'].">";
echo '<button type="button" class="btn btn-danger deletebt" οnclick="return deleteFn(this)">删除</button></tr></td></form>';
}
?>
</table>
</div>
</div>
</body>
<script src="js/jquery-3.5.1.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/jquery-3.5.1.js"></script>
<script type="text/javascript" >
function deleteFn(form){
let formData = new FormData(form);
let id = formData.get('id');
console.log(id);
var con;
con=confirm("确定要删除该用户吗?");
if(con==true){
$.ajax({
url: 'delete.php',
type:'POST',
data:{id:id},
success:function(result){
console.log(result);
location.reload()
}
});
}
}
</script>
</html>