Select / SelectAll delete Not working in PHP MySQL / AJAX Jquery -
good day, can't seem understand why isn't working. i've set files able read database, won't delete. managed select button work, won't let me delete.
here's code.
php delete
<?php session_start(); include_once('dbconfig.php'); if(isset($_post['bulk_delete_submit'])){ $idarr = $_post['checked_id']; foreach($idarr $id){ mysqli_query($conn,"delete appointments id=".$id); } $_session['success_msg'] = 'users have been deleted successfully.'; header("location:display.php"); } ?>
here's data being sent display page.
<html> <head> <script type="text/javascript" src="delete.js"></script> </head> <body> <?php session_start(); if(!empty($_session['success_msg'])){ ?> <div class="alert alert-success"><?php echo $_session['success_msg']; ?></div> <?php unset($_session['success_msg']); } ?> <?php include_once('dbconfig.php'); $query = mysqli_query($conn,"select * appointments"); ?> <?php $connect = mysqli_connect("localhost", "root", "root", "appointments"); $output = ''; $sql = "select * appointments order id desc"; $result = mysqli_query($connect, $sql); $output .= ' <form name="bulk_action_form" action="delete_multiple.php" method="post" onsubmit="return delete_confirm();"/> <div class="table-responsive"> <table class="table table-bordered"> <tr> <th><input type="checkbox" name="select_all" id="select_all" value=""/></th> <th width="10%">id</th> <th width="40%">name</th> <th width="40%">email</th> <th width="40%">address</th> <th width="10%">phonenumber</th> <th width="10%">appointmenttime</th> <th width="10%">appointmentdate</th> <th width="50%">message</th> </tr>'; if(mysqli_num_rows($result) > 0) { while($row = mysqli_fetch_assoc($result)) { $output .= ' <tr data-row-id="'.$row["id"].'"> <td align="center"><input type="checkbox" name="checked_id[]" class="checkbox" value="'.$row["id"].'"/></td> <td>'.$row["id"].'</td> <td class="name" data-id1="'.$row["id"].'" contenteditable>'.$row["name"].'</td> <td class="email" data-id2="'.$row["id"].'" contenteditable>'.$row["email"].'</td> <td class="address" data-id2="'.$row["id"].'" contenteditable>'.$row["address"].'</td> <td class="phonenumber" data-id2="'.$row["id"].'" contenteditable>'.$row["phonenumber"].'</td> <td class="appointmenttime" data-id2="'.$row["id"].'" contenteditable>'.$row["appointmenttime"].'</td> <td class="appointmentdate" data-id2="'.$row["id"].'" contenteditable>'.$row["appointmentdate"].'</td> <td class="message" data-id2="'.$row["id"].'" contenteditable>'.$row["message"].'</td> </tr> '; } } else { $output .= '<tr><td colspan="10">data not found</td></tr>'; } $output .= '</table> <div align="center"> <input type="submit" class="btn btn-danger" name="bulk_delete_submit" value="delete"/> </div> </div> '; echo $output; ?> </body> </html>
here's javascript
function delete_confirm(){ var result = confirm("are sure delete users?"); if(result){ return true; }else{ return false; } } $(document).ready(function(){ $('#select_all').on('click',function(){ if(this.checked){ $('.checkbox').each(function(){ this.checked = true; }); }else{ $('.checkbox').each(function(){ this.checked = false; }); } }); $('.checkbox').on('click',function(){ if($('.checkbox:checked').length == $('.checkbox').length){ $('#select_all').prop('checked',true); }else{ $('#select_all').prop('checked',false); } }); }); $(document).ready(function(){ $('#select_all').on('click',function(){ if(this.checked){ $('.checkbox').each(function(){ this.checked = true; }); }else{ $('.checkbox').each(function(){ this.checked = false; }); } }); $('.checkbox').on('click',function(){ if($('.checkbox:checked').length == $('.checkbox').length){ $('#select_all').prop('checked',true); }else{ $('#select_all').prop('checked',false); } }); });
also, if may.. here's display page js of table found.
<?php require("config.php"); if(empty($_session['user'])) { header("location: success.php"); die("redirecting index.php"); } ?> <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="x-ua-compatible" content="ie=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="description" content=""> <meta name="author" content=""> <title></title> <link href="css/bootstrap.min.css" rel="stylesheet"> <link href="css/simple-sidebar.css" rel="stylesheet"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script> </head> <body> <div id="wrapper"> <!-- sidebar --> <div id="sidebar-wrapper"> <ul class="sidebar-nav"> <li class="sidebar-brand"> <a href="#"> hope medi clinic </a> </li> <li> <a href="logout.php">logout</a> </li> <li> <a href="../../appointments/">main website</a> </li> </ul> </div> <!-- /#sidebar-wrapper --> <!-- page content --> <div class="container"> <br /> <br /> <br /> <div class="table-responsive"> <h3 align="center">appointments</h3><br /> <div id="live_data"></div> <a href="#menu-toggle" class="btn btn-default" id="menu-toggle">toggle menu</a> </div> </div> <!-- /#page-content-wrapper --> </div> <script> $(document).ready(function(){ function fetch_data() { $.ajax({ url:"select.php", method:"post", success:function(data){ $('#live_data').html(data); } }); } fetch_data(); function edit_data(id, text, column_name) { $.ajax({ url:"edit.php", method:"post", data:{id:id, text:text, column_name:column_name}, datatype:"text", success:function(data){ alert(data); } }); } /* ............. */ $(document).on('blur', '.name', function(){ var id = $(this).data("id1"); var name = $(this).text(); edit_data(id, name, "name"); }); $(document).on('blur', '.email', function(){ var id = $(this).data("id2"); var email = $(this).text(); edit_data(id, email, "email"); }); $(document).on('blur', '.address', function(){ var id = $(this).data("id2"); var address = $(this).text(); edit_data(id, address, "address"); }); $(document).on('blur', '.phonenumber', function(){ var id = $(this).data("id2"); var phonenumber = $(this).text(); edit_data(id, phonenumber, "phonenumber"); }); $(document).on('blur', '.appointmenttime', function(){ var id = $(this).data("id2"); var appointmenttime = $(this).text(); edit_data(id, appointmenttime, "appointmenttime"); }); $(document).on('blur', '.appointmentdate', function(){ var id = $(this).data("id2"); var appointmentdate = $(this).text(); edit_data(id, appointmentdate, "appointmentdate"); }); $(document).on('blur', '.message', function(){ var id = $(this).data("id2"); var message = $(this).text(); edit_data(id, message, "message"); }); $(document).on('click', '.btn_delete', function(){ var id=$(this).data("id3"); if(confirm("are sure want delete this?")) { $.ajax({ url:"delete.php", method:"post", data:{id:id}, datatype:"text", success:function(data){ alert(data); fetch_data(); } }); } }); }); </script> <script> $("#menu-toggle").click(function(e) { e.preventdefault(); $("#wrapper").toggleclass("toggled"); }); </script> </body> </html>
questions:
- why did repeat script third given code?
- what purpose of ajax in last given code if have script delete confirmation in third given code?
- i assume first given code
delete_multiple.php
- where button has class tag of
btn_delete
? - do receive error browser's console?
first, lets assume delete
button below user's table batch deletion. remove redundant script , make sure include jquery file because running jquery script.
<script src="jquery-1.9.1.min.js"></script>
then want have delete button each row? purpose of ajax in last given code, right?
lets add new column delete button (single deletion):
.... <td class="appointmentdate" data-id2="'.$row["id"].'" contenteditable>'.$row["appointmentdate"].'</td> <td class="message" data-id2="'.$row["id"].'" contenteditable>'.$row["message"].'</td> <td><a href="#" data-artid="'.$row["id"].'" class="btn_delete"><span class="glyphicon glyphicon-remove"></span></a></td> ....
so when btn_delete
clicked, delete row:
$(document).on('click', '.btn_delete', function(){ var elem = $(this), id = elem.data("data-artid"); if(confirm("are sure want delete this?")) { $.ajax({ url:"delete.php", method:"post", data:{id:id}, datatype:"text", success:function(data){ elem.closest("tr").hide(200); } }); } });
so in delete.php
, have delete query:
mysqli_query($conn,"delete appointments id=".$_post["id"]);
suggestions:
- use @ least
*_real_escape_string()
- or better yet, use prepared statement
Comments
Post a Comment