Node.js:myweb(php+node无刷新删除)

13 篇文章 0 订阅

index.php

<?php 
$servername = "localhost";
$username = "root";
$password = "133nubia022";
$dbname = "myweb";
 
// 创建连接
$conn = mysqli_connect($servername, $username, $password,$dbname);
 


$sql = "select * from user";
$rst = mysqli_query($conn,$sql);
 ?>
<!doctype html>
<html>
<head>
	<meta charset="UTF-8">
	<title>index</title>
	<style>
		*{
			font-family: 微软雅黑;
		}
	</style>
	<link rel="stylesheet" href="bs/css/bootstrap.min.css">
	<script src='bs/js/jquery.min.js'></script>
	<script src='bs/js/bootstrap.min.js'></script>
</head>
<body>
	<div class="container">
		<h2 class='page-header'>查看用户:</h2>
		<table class='table table-bordered table-hover'>
			<tr>
				<th>编号</th>
				<th>用户名</th>
				<th>密码</th>
				<th>删除</th>
			</tr>
			<?php 
			while($row=mysqli_fetch_assoc($rst)){
				echo "<tr>";
				echo "<td>{$row['id']}</td>";
				echo "<td>{$row['usename']}</td>";
				echo "<td>{$row['password']}</td>";
				echo "<td><a href='javascript:' class='del' id='{$row['id']}'>删除</a></td>";
				echo "</tr>";
			}
			 ?>
		</table>
	</div>	
</body>
<script>
$('.del').click(function(){
	id=this.id;
	obj=$(this);
	$.getJSON('http://localhost:8888?cb=?',{id:id},function(json){
       console.warn(json);
        
		if(json.ok=='1'){
			obj.parent().parent().hide(1000);
		}
	});
});
</script>
</html>


node安装mysql

在D:\xampp\htdocs\node\node
npm install mysql

index.js

http = require('http');
url = require('url');
querystring = require('querystring');
mysql = require('mysql');

cs = function(req, res) {
    uri = req.url;
    json = querystring.parse(url.parse(uri).query);

    fname = json.cb;
    id = json.id;
    jsonstr = fname + '({"ok":"1"})';

    //连接、操作数据库
    conn = mysql.createConnection({
        host: 'localhost',
        user: 'root',
        password: '133nubia022',
        database: 'myweb'
    });

    // 操作数据库
    conn.connect();
    conn.query('delete from user where id=' + id, function(err, rs) {
        if (rs.affectedRows == '1') {
            //给前端jsonp返回成功删除的消息{ok:1}	
            res.write(jsonstr);

            //关闭响应
            res.end();
        }
    });

    //关闭数据库
    conn.end();
}

http.createServer(cs).listen(8888);

**



**



**



**



**



**



**



**



**



**



**



**



**


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值