美好的一天,我似乎無法理解爲什麼這不起作用。我已經將所有文件設置爲可以從我的數據庫讀取,但它不會被刪除。我設法讓全選按鈕工作,但它不會讓我刪除。選擇/ SelectAll刪除不能在PHP MySQL/AJAX中工作Jquery
這是我的代碼。
PHP刪除
<?php
session_start();
include_once('dbConfig.php');
if(isset($_POST['bulk_delete_submit'])){
$idArr = $_POST['checked_id'];
foreach($idArr as $id){
mysqli_query($conn,"DELETE FROM appointments WHERE id=".$id);
}
$_SESSION['success_msg'] = 'Users have been deleted successfully.';
header("Location:display.php");
}
?>
這裏的數據被髮送到顯示頁面。
<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 * FROM appointments");
?>
<?php
$connect = mysqli_connect("localhost", "root", "root", "appointments");
$output = '';
$sql = "SELECT * FROM appointments ORDER BY 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>
這裏是JavaScript的
function delete_confirm(){
\t var result = confirm("Are you sure to delete users?");
\t if(result){
\t \t return true;
\t }else{
\t \t return false;
\t }
}
$(document).ready(function(){
$('#select_all').on('click',function(){
if(this.checked){
$('.checkbox').each(function(){
this.checked = true;
});
}else{
$('.checkbox').each(function(){
this.checked = false;
});
}
});
\t
\t $('.checkbox').on('click',function(){
\t \t if($('.checkbox:checked').length == $('.checkbox').length){
\t \t \t $('#select_all').prop('checked',true);
\t \t }else{
\t \t \t $('#select_all').prop('checked',false);
\t \t }
\t });
});
$(document).ready(function(){
$('#select_all').on('click',function(){
if(this.checked){
$('.checkbox').each(function(){
this.checked = true;
});
}else{
$('.checkbox').each(function(){
this.checked = false;
});
}
});
\t
\t $('.checkbox').on('click',function(){
\t \t if($('.checkbox:checked').length == $('.checkbox').length){
\t \t \t $('#select_all').prop('checked',true);
\t \t }else{
\t \t \t $('#select_all').prop('checked',false);
\t \t }
\t });
});
另外,如果可以的話..這裏的地方臺的JS發現顯示頁面。
<?php
require("config.php");
if(empty($_SESSION['user']))
{
header("Location: success.php");
die("Redirecting to 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 you sure you want to 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>
請張貼的print_r的'結果($ _ POST [ 'checked_id []'])' – Boby
爲什麼你從第三個給定的代碼重複你的腳本?如果您的第三個給定代碼中已經有用於刪除確認的腳本,那麼您最後給出的代碼中的AJAX的目的是什麼?我假設你的第一個代碼是'delete_multiple.php'。你的按鈕有一個'btn_delete'類標籤在哪裏? –
@LoganWayne對不起,我忘了刪除,當我粘貼我的代碼在這裏。它實際上是用於單行刪除,並且與多重刪除功能無關。至於按鈕,我從代碼中刪除它.. –