gpt4 book ai didi

php - 删除一行后进入同一个数据页

转载 作者:可可西里 更新时间:2023-11-01 07:39:08 25 4
gpt4 key购买 nike

我有一个表格,其中显示了特定日期的行数。我想在删除一行后返回到同一页面并显示同一日期的同一行。示例:如果日期为 06/06/2015 我有 2 行并且我删除了其中一个我想返回到同一页面并查看另一行。我有这些代码:

删除.php:

<?php 
require_once ('../include/global.php');
$id=$_REQUEST['id'];
$sql = "DELETE FROM appoint WHERE id=".$id;
$result=mysqli_query($con,$sql) or die('Unable to execute query. '. mysqli_error($con));
if($result){
$sql2 = "SELECT date FROM appoint WHERE id=".$id;
$result2=mysqli_query($con,$sql2) or die('Unable to execute query. '. mysqli_error($con));
$rows = mysqli_fetch_array($con, $result2);
header("location:appoint.php");


}
else {
header("location:../update/update_false.php");
}
?>

获取数据.php:

<?php
require_once ('../include/global.php');
if($_POST['seldate']) {
$selDate = $_POST['seldate'];
$sql="SELECT * FROM clinic.appoint WHERE date='$selDate'";
$result = mysqli_query($con, $sql) or die($sql."<br/><br/>".mysql_error());
while($rows=mysqli_fetch_array($result)){
?>
<tr>
<td scope="row"><?php echo $rows['time'] ?></td>
<td scope="row"><?php echo $rows['name'] ?></td>
<td scope="row"><?php echo $rows['date'] ?></td>
<td scope="row"><form action='/clinic form/appoint/delete.php'=<?php echo $rows['id']; ?>' method="post">
<input type="hidden" name="id" value="<?php echo $rows['id']; ?>">
<input type="submit" name="submit1" value="Done">
</form>

</td>
</tr>
<?php } } ?>

以及数据展示表页面指定.php:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function(){
$("#Date").change(function(){
var seldate =$(this).val();
$("#scheduleDate").html(seldate);
var dataString = 'seldate='+ seldate;
$.ajax({
type: "POST",
url: "getdata.php",
data: dataString,
cache: false,
success: function(data) {
$("#Schedule").html(data);
}
});
});
});
</script>
</head>

<body>
<div class="container12">
<header>
<div align="center">
<div class="column12"> <a href="homepage.php"><img src="images/logo.png"/></a> </div>
</div>
<div align="center">
<div align="center"><a href="logout.php">Logout</a></div>
</div>
</header>
<h1 id="home">&nbsp;</h1>
<div class="alert"></div>
<div class="column12" align="left"><B><center>Appointments</B></center><br />
</div>
</div>
</div>
</div>
<div class="container12" align="center">
<form action="addApp.php" method="post" name="form1" id="form1">
<table width="700px" class="imagetable" align="center">
<th align="center">Name</th>
<th>Date</th>
<th>Time</th>
<th>Contact Number</th>
<th>Action</th>
<tr>
<td><input type="text" name="name" class="large-fld-app" placeholder="name"/></td>
<td><input type="text" name="date" class="large-fld-app" placeholder="DD/MM/YYYY"/></td>
<td><input type="time" name="time" class="large-fld-app"/></td>
<td><input type="text" name="phone" class="large-fld-app"/></td>
<td align="center"><input type="submit" value="" name="submit" class="imgClass_save" />
</td>
</tr>
</table>
</form>
</div>
<?php
//$sql="SELECT * FROM clinic.appoint";
//$result = mysqli_query($con, $sql) or die($sql."<br/><br/>".mysql_error());
//$rows=mysqli_fetch_array($result)
?>
<div class="container">
<table>
<thead>
<tr>
Schedule
<th scope="row">
<select name="Date" required class="form-control" id="Date">
<option value="">Please Select Date</option>
<?php $sql2="SELECT * FROM clinic.appoint GROUP BY date";
$result2 = mysqli_query($con, $sql2) or die($sql2."<br/><br/>".mysql_error());
while($rows2=mysqli_fetch_array($result2)){?>
<option value="<?php echo $rows2['date'] ?>"><?php echo $rows2['date'] ?></option>
<?php } ?>
</select>
</th>
<td class="schedule-offset" colspan="2" id="scheduleDate"></td>
</tr>
</thead>
<tbody id="Schedule"></tbody>
</table>
</div>
</body>
</html>

最佳答案

只是一个快速的解决方案:将您的 ajax 部分放入一个函数中。

更改用于删除的 PHP 代码:

require_once ('../include/global.php');
$id = $_REQUEST['id'];

# Find the date for which you are deleting...
$query = "SELECT date FROM appoint WHERE id = " . $id;
$row = mysqli_fetch_assoc(mysqli_query($con, $query));
$request_date = $row['date'];

$sql = "DELETE FROM appoint WHERE id=".$id;
$result = mysqli_query($con,$sql) or die('Unable to execute query. '. mysqli_error($con));
if($result){
header("Location: appoint.php?date=" . $request_date);
}
else {
header("Location: ../update/update_false.php");
}

现在,当您从删除中返回时,您有一个 $_GET['date']。但是当你第一次加载页面时,你并没有那个。

现在更改您的 JS 以适应它。将 AJAX 部分放在一个函数中。

$(document).ready(function(){
$("#Date").change(function(){
var seldate =$(this).val();
display_data(seldate);
});

// This is the function...
function display_data(seldate) {

$("#scheduleDate").html(seldate);
var dataString = 'seldate='+ seldate;
$.ajax({
type: "POST",
url: "getdata.php",
data: dataString,
cache: false,
success: function(data) {
$("#Schedule").html(data);
}
});

}

// Now here is the real code for retaining your Date...
<?php
if (!empty($_GET['date'])) {
?>
display_data('<?php echo $_GET["date"]; ?>')
<?php
}
?>
document.getElementById('Date').value = '<?php echo @$_GET["date"]; ?>';

});

现在您只需在 dom 准备就绪的特定日期调用 display_data() 函数即可。

您还可以使用 session :

示例:在我的 php 代码中,将 $request_date 存储在 session 中。

如果您还没有启动 session ,请先启动它 session_start()。在所有页面中,HTML 和 DELETE PHP 以及 UPDATE FAIL 页面。

$request_date = $row['date'];
$_SESSION['date'] = $request_date;

并将重定向部分改回您的代码。

header("Location: appoint.php");

现在更改我的脚本 block 的这一部分:

来自:

<?php
if (!empty($_GET['date'])) {
?>
display_data('<?php echo $_GET["date"]; ?>');
<?php
}
?>
document.getElementById('Date').value = '<?php echo @$_GET["date"]; ?>';

收件人:

<?php
if (!empty($_SESSION['date'])) {
?>
display_data('<?php echo $_SESSION["date"]; ?>');
<?php
unset($_SESSION['date']);
}
?>
document.getElementById('Date').value = '<?php echo @$_SESSION["date"]; ?>';

使用 session 的好处是,如果您删除失败并被重定向到更新失败页面。之后,当您再次返回列表页面时。您将回到离开该列表的位置。

关于php - 删除一行后进入同一个数据页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33330221/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com