mysql+php+ajax实现用户管理(简单版,日期处理)
2017-11-17 10:52
591 查看
user.html
<div id="right">
<div id="right_head">
<p>
<span>用户管理</span>
<span class="right_head_span1">欢迎:</span>
<span>XXX</span>
<a href="" class="right_head_span2">退出</a>
</p>
</div>
<div id="right_con">
<table border="1" cellspacing="0">
<thead>
<tr>
<td>用户名</td>
<td>分组</td>
<td>邮箱</td>
<td>手机号</td>
<td>注册时间</td>
<td>操作</td>
</tr>
</thead>
<tbody id="tby">
</tbody>
<tfoot>
<tr>
<td>
<a class="tfoot_a" onclick="getPage(1)">首页</a>
</td>
<td>
<a class="tfoot_a" onclick="prePage()">上一页</a>
</td>
<td>
<a class="tfoot_a" onclick="nextPage()">下一页</a>
</td>
<td>
<a class="tfoot_a" onclick="lastPage()">尾页</a>
</td>
<td>
<p>
转到<input type="text" id="in_pNum">页
<button onclick="goPage()">GO</button>
</p>
</td>
<td class="foot_last_td">
第<span id="tfoot_span1"></span>页/共<span id="tfoot_span2"></span>页
</td>
</tr>
</tfoot>
</table>
</div>
</div>
<script src="js/common.js"></script>
<script>
function getPage(num) {
var xhr = createXhr();
xhr.open("get","php/user.php?currentPage="+num,true);
console.log("1111");
xhr.onreadystatechange = function(){
if(xhr.readyState == 4 && xhr.status == 200){
var resText = xhr.responseText;
console.log(resText);
if(resText != "0"){
var arr = JSON.parse(resText);
var str = "";
var pageInfo = JSON.parse(arr[arr.length-1]);
for(var i=0;i<arr.length-1;i++){
str += "<tr>";
str += "<td>" + arr[i].uname + "</td>";
if(arr[i].groups == 1)
str += "<td>" + "管理员" + "</td>";
else if(arr[i].groups == 0)
str += "<td>" + "普通用户" + "</td>";
str += "<td>" + arr[i].email + "</td>";
str += "<td>" + arr[i].phone + "</td>";
str += "<td>" + arr[i].reg_time + "</td>";
str += "<td><a class=\"tfoot_a\" onclick=\"deleteUser(" + num + "," + arr[i].uid + ")\">删除</a>" + "</td>";
str += "</tr>";
}
$("tby").innerHTML = str;
$("tfoot_span1").innerHTML = pageInfo.currentPage;
$("tfoot_span2").innerHTML = pageInfo.totalPage;
}else{
$("tby").innerHTML = "";
}
}
}
xhr.send(null);
}
function prePage() {
console.log("prePage");
var currentPage = Number($("tfoot_span1").innerHTML);
getPage(currentPage-1);
}
function nextPage() {
console.log("nextPage");
var currentPage = Number($("tfoot_span1").innerHTML);
getPage(currentPage+1);
}
function lastPage() {
console.log("lastPage");
var lastPage = Number($("tfoot_span2").innerHTML);
getPage(lastPage);
}
function goPage() {
console.log("goPage");
var pageNum = Number($("in_pNum").value);
console.log(pageNum);
getPage(pageNum);
}
function deleteUser(num,uid) { //加一个页面参数 并刷新该页面
//alert("确定删除?");
var xhr = createXhr();
xhr.open("get","php/deleteUser.php?uid="+uid,false);
xhr.onreadystatechange = function(){
if(xhr.readyState == 4 && xhr.status == 200){
var resText = xhr.responseText;
console.log(resText);
if(resText == "1"){
console.log("删除成功" + num);
getPage(num);
}else if(resText == "0"){
console.log("无数据");
getPage(num);
}
}
}
xhr.send(null);
}
window.onload = function() {
getPage(1);
}
</script>
</body>
</html>
user.php
<?php
require("init.php");
@$currentPage = $_REQUEST["currentPage"];
if($currentPage == "" || $currentPage == null){
$currentPage = 1;
}
@$pageSize = $_REQUEST["pageSize"];
if($pageSize == "" || $pageSize == null){
$pageSize = 10;
}
$sql = "SELECT count(*) FROM xs_user";
$result = mysqli_query($conn,$sql);
$arr = mysqli_fetch_row($result);
$rowCount = $arr[0];
//echo $rowCount;
$totalPage = ceil($rowCount/$pageSize);
if($currentPage > $totalPage){
$currentPage = $totalPage;
}
if($currentPage < 1){
$currentPage = 1;
}
$start = ($currentPage - 1) * $pageSize;
$sql = "SELECT * FROM xs_user LIMIT $start,$pageSize";
$result = mysqli_query($conn,$sql);
if($result === false){
echo "异常,请检查SQL语句:";
echo $sql;
}else{
$count = mysqli_affected_rows($conn);
if($count == 0){
echo 0;
}else if($count > 0){
$arr = mysqli_fet
dbbb
ch_all($result,1);
if($result === false){
echo "异常,请检查SQL语句:";
echo $sql;
}else{
//var_dump($arr);
for($i=0;$i<count($arr);$i++){
$arr[$i]["reg_time"] = date("Y-m-d H:i",$arr[$i]["reg_time"]);
}
$lastStr = "{\"currentPage\":$currentPage,\"totalPage\":$totalPage}";
Array_push($arr,$lastStr);
$arr = json_encode($arr);
echo $arr;
}
}
}
?>
deleteUser.php
<?php
require("init.php");
@$uid = $_REQUEST["uid"];
$sql = "DELETE FROM xs_user WHERE uid = '$uid'";
$result = mysqli_query($conn,$sql);
if($result === false){
echo "异常,请检查SQL语句:";
echo $sql;
}else{
$count = mysqli_affected_rows($conn);
if($count == 0){
echo 0;
}else if($count > 0){
echo 1;
}
}
?>
<div id="right">
<div id="right_head">
<p>
<span>用户管理</span>
<span class="right_head_span1">欢迎:</span>
<span>XXX</span>
<a href="" class="right_head_span2">退出</a>
</p>
</div>
<div id="right_con">
<table border="1" cellspacing="0">
<thead>
<tr>
<td>用户名</td>
<td>分组</td>
<td>邮箱</td>
<td>手机号</td>
<td>注册时间</td>
<td>操作</td>
</tr>
</thead>
<tbody id="tby">
</tbody>
<tfoot>
<tr>
<td>
<a class="tfoot_a" onclick="getPage(1)">首页</a>
</td>
<td>
<a class="tfoot_a" onclick="prePage()">上一页</a>
</td>
<td>
<a class="tfoot_a" onclick="nextPage()">下一页</a>
</td>
<td>
<a class="tfoot_a" onclick="lastPage()">尾页</a>
</td>
<td>
<p>
转到<input type="text" id="in_pNum">页
<button onclick="goPage()">GO</button>
</p>
</td>
<td class="foot_last_td">
第<span id="tfoot_span1"></span>页/共<span id="tfoot_span2"></span>页
</td>
</tr>
</tfoot>
</table>
</div>
</div>
<script src="js/common.js"></script>
<script>
function getPage(num) {
var xhr = createXhr();
xhr.open("get","php/user.php?currentPage="+num,true);
console.log("1111");
xhr.onreadystatechange = function(){
if(xhr.readyState == 4 && xhr.status == 200){
var resText = xhr.responseText;
console.log(resText);
if(resText != "0"){
var arr = JSON.parse(resText);
var str = "";
var pageInfo = JSON.parse(arr[arr.length-1]);
for(var i=0;i<arr.length-1;i++){
str += "<tr>";
str += "<td>" + arr[i].uname + "</td>";
if(arr[i].groups == 1)
str += "<td>" + "管理员" + "</td>";
else if(arr[i].groups == 0)
str += "<td>" + "普通用户" + "</td>";
str += "<td>" + arr[i].email + "</td>";
str += "<td>" + arr[i].phone + "</td>";
str += "<td>" + arr[i].reg_time + "</td>";
str += "<td><a class=\"tfoot_a\" onclick=\"deleteUser(" + num + "," + arr[i].uid + ")\">删除</a>" + "</td>";
str += "</tr>";
}
$("tby").innerHTML = str;
$("tfoot_span1").innerHTML = pageInfo.currentPage;
$("tfoot_span2").innerHTML = pageInfo.totalPage;
}else{
$("tby").innerHTML = "";
}
}
}
xhr.send(null);
}
function prePage() {
console.log("prePage");
var currentPage = Number($("tfoot_span1").innerHTML);
getPage(currentPage-1);
}
function nextPage() {
console.log("nextPage");
var currentPage = Number($("tfoot_span1").innerHTML);
getPage(currentPage+1);
}
function lastPage() {
console.log("lastPage");
var lastPage = Number($("tfoot_span2").innerHTML);
getPage(lastPage);
}
function goPage() {
console.log("goPage");
var pageNum = Number($("in_pNum").value);
console.log(pageNum);
getPage(pageNum);
}
function deleteUser(num,uid) { //加一个页面参数 并刷新该页面
//alert("确定删除?");
var xhr = createXhr();
xhr.open("get","php/deleteUser.php?uid="+uid,false);
xhr.onreadystatechange = function(){
if(xhr.readyState == 4 && xhr.status == 200){
var resText = xhr.responseText;
console.log(resText);
if(resText == "1"){
console.log("删除成功" + num);
getPage(num);
}else if(resText == "0"){
console.log("无数据");
getPage(num);
}
}
}
xhr.send(null);
}
window.onload = function() {
getPage(1);
}
</script>
</body>
</html>
user.php
<?php
require("init.php");
@$currentPage = $_REQUEST["currentPage"];
if($currentPage == "" || $currentPage == null){
$currentPage = 1;
}
@$pageSize = $_REQUEST["pageSize"];
if($pageSize == "" || $pageSize == null){
$pageSize = 10;
}
$sql = "SELECT count(*) FROM xs_user";
$result = mysqli_query($conn,$sql);
$arr = mysqli_fetch_row($result);
$rowCount = $arr[0];
//echo $rowCount;
$totalPage = ceil($rowCount/$pageSize);
if($currentPage > $totalPage){
$currentPage = $totalPage;
}
if($currentPage < 1){
$currentPage = 1;
}
$start = ($currentPage - 1) * $pageSize;
$sql = "SELECT * FROM xs_user LIMIT $start,$pageSize";
$result = mysqli_query($conn,$sql);
if($result === false){
echo "异常,请检查SQL语句:";
echo $sql;
}else{
$count = mysqli_affected_rows($conn);
if($count == 0){
echo 0;
}else if($count > 0){
$arr = mysqli_fet
dbbb
ch_all($result,1);
if($result === false){
echo "异常,请检查SQL语句:";
echo $sql;
}else{
//var_dump($arr);
for($i=0;$i<count($arr);$i++){
$arr[$i]["reg_time"] = date("Y-m-d H:i",$arr[$i]["reg_time"]);
}
$lastStr = "{\"currentPage\":$currentPage,\"totalPage\":$totalPage}";
Array_push($arr,$lastStr);
$arr = json_encode($arr);
echo $arr;
}
}
}
?>
deleteUser.php
<?php
require("init.php");
@$uid = $_REQUEST["uid"];
$sql = "DELETE FROM xs_user WHERE uid = '$uid'";
$result = mysqli_query($conn,$sql);
if($result === false){
echo "异常,请检查SQL语句:";
echo $sql;
}else{
$count = mysqli_affected_rows($conn);
if($count == 0){
echo 0;
}else if($count > 0){
echo 1;
}
}
?>
相关文章推荐
- Jquery+Ajax+PHP+MySQL实现分类列表管理(下)
- Jquery+Ajax+PHP+MySQL实现分类列表管理(下)
- 采用Jquery+Ajax+PHP+MySQL实现分类列表管理 - 上篇
- PHP连接Mysql,实现基本的增、删、改、查——用户管理系统
- Jquery+Ajax+PHP+MySQL实现分类列表管理(上)
- Jquery+Ajax+PHP+MySQL实现分类列表管理(上)
- 采用Jquery+Ajax+PHP+MySQL实现分类列表管理 - 下篇
- 使用AJAX+php+mysql实现省市县三级联动
- 初识SQLyogEnt:PHP用户的MySQL管理工具
- jQuery(Ajax)/PHP/MySQL实现搜索自动提示功能
- 使用ajax+dom+php+mysql实现多级联动(省市县)
- Ajax+js+dom+json+php+mysql实现仿google的超强分页、隔行变色效果
- ajax+js+dom+json+php+mysql实现仿百度、google的分页隔行变色效果(加图片)
- 用php+mysql+json+js+ajax实现省市县三级联动
- ajax+js+dom+json+php+mysql实现仿百度、google的分页效果
- 使用AJAX+php+mysql实现省市县三级联动
- 【转】使用 jQuery(Ajax)/PHP/MySQL实现自动完成功能
- 用PHP实现Ftp用户的在线管理
- PHP+jQuery+Ajax实现用户登录与退出
- ajax+dom+php+mysql实现多级联动效果