ajax向php传参数对数据库操作

刚入门php,要求要对多用户进行批量删除(当然实际中是不可能的),在这就以此为例。

大意就是通过对数据库中用户查询,将用户信息显示在页面表格中,在进行多项选择后将所选行参数通过ajax传入后台php文件,

进行对用户的删除操作!

首先访问页面时,得到数据库中用户进行显示

对数据库中查询结果循环输出到表格。jquery实现全选、反选、取消操作。ajax获取选定数据传入后台。

 1 $sqlstr="select * from stuinfo";
 2     $result=mysqli_query($conn,$sqlstr);
 3     if(mysqli_num_rows($result)>0){
 4     ?>
 5     <table border="2px" cellpadding="5px" cellspacing="0">
 6         <thead>
 7             <td>头像</td><td>学号</td><td>姓名</td><td>职务</td><td>操作</td>
 8             <td>
 9                 批量<br />
10                 <label id="chooseAll">全选 |</label>
11                 <label id="reverseAll">反选 |</label>
12                 <label id="cancleAll">取消</label>
13             </td>
14         </thead>
15         <tbody>
16 <?php
17     while($row=mysqli_fetch_assoc($result)){
18         echo "
19             <tr>
20                 <td><img id='icon' src=".$row['icon']."></td>
21                 <td>".$row['userid']."</td>
22                 <td>".$row['username']."</td>
23                 <td>".$row['job']."</td>
24                 <td><a href='del.php?id=".$row['id']."target='_black'>删除</a></td>
25                 <td><input type='checkbox' name='del' value=".$row['id']." /></td>
26             </tr>";    
27 }?>
28         </tbody>
29     </table>
30 <input id="php_sub" type="button" value="删除勾选" >
PHP显示用户列表
 1 <script>
 2     $('#chooseAll').click(function () {
 3         $('tbody :checkbox').prop('checked',true);
 4     });
 5     $('#cancleAll').click(function () {
 6         $('tbody :checkbox').prop('checked',false);
 7     });
 8     $('#reverseAll').click(function () {
 9         $('tbody :checkbox').each(function(){
10             if($(this).prop('checked')){
11                 $(this).prop('checked',false);
12             }else{
13                 $(this).prop('checked',true);
14             }
15         });
16     });
js全、反选,取消操作
 1 $('#php_sub').click(function(){
 2         var ids=new Array();
 3         var i=0;
 4         $('tbody :checkbox').each(function(){
 5             if($(this).prop('checked')){
 6                 ids[i++]=$(this).val();
 7             }
 8         });
 9         if(confirm("确定删除选中?")){
10             $.ajax({
11                 type:"POST",
12                 async:false,
13                 url:'del.php',
14                 data:{ids:ids},
15                 success:function(){
16                     $('tbody :checkbox').each(function(){
17                         if($(this).prop('checked')){
18                      $(this).parent().parent().remove();
19                 }
20                 });
21                     
22                 }
23 
24             });
25         }
26     });
ajax传参数
1 $ids=$_POST['ids'];
2         foreach ($ids as $key => $value) {
3             $sqlstr="delete from stuinfo where id='".$value."'";
4             mysqli_query($conn,$sqlstr);
5         }
php获取参数操作数据库

 

posted @ 2018-03-22 19:42  x_smile  阅读(460)  评论(0编辑  收藏  举报