php操作数据之异步添加删除
php操作数据之异步添加删除add.html
<body>
<script src='./js/jquery-3.3.1.min.js'></script>
<script>
$(document).ready(function() {
$(':button:first').click(function(){
var title=$('#title').val();
var content=$('#content').val();
$.post('./add.php',{'title':title,'content':content},function(data){
if(data){
alert('添加成功');
location.href='list.php';
}else{
alert('添加失败');
}
})
})
});
</script>
<!--1、创建表单-->
<form method="post" action="">
标题: <input type="text" id="title"> <br /> <br />
内容:<textarea id="content" rows="5" cols="30"></textarea> <br /> <br />
<input type="button" name="button" value="提交">
</form>
</body>add.php
<?php
require './inc/conn.php';
$title=$_POST['title'];
$content=$_POST['content'];
$time=time();
$sql="insert into news values (null,'$title','$content',$time)";
echo mysqli_query($link,$sql)?1:0;异步删除list.php-- button按钮
<td><input type="button" value="删除" op='del' newsid=<?php echo $rows['id']?>></td>
<script src='./js/jquery-3.3.1.min.js'></script>
<script>
$(document).ready(function() {
$(':button').click(function(){
var tr=$(this).parents('tr'); //按钮所在的行
if($(this).attr('op')=='del'){
var id=$(this).attr('newsid'); //获取新闻的id
$.post('./del.php',{'id':id},function(data){
if(data==1)
tr.remove();
else
alert('删除失败');
})
}
})
});
</script>del.php
<?php
//1、连接数据库
require './inc/conn.php';
//2、拼接SQL语句
$sql="delete from news where id={$_POST['id']}";
//3、执行SQL语句
echo mysqli_query($link,$sql)?1:0;
页:
[1]