
本文介绍了一种在PHP和JavaScript环境中,提交表单后隐藏已选中表格行的方法,而无需从数据库中删除数据。通过在数据库中添加一个布尔类型的字段,并在提交时更新该字段,可以实现选择行的隐藏功能。同时,在前端通过PHP条件判断来控制行的显示,从而达到预期的效果。
核心思路是在数据库表中增加一个用于标记行是否已被选中的字段(例如 checkbox),默认值为 0 或 NULL。当用户提交表单后,通过 PHP 更新数据库中对应行的该字段值为 1。在生成表格时,使用 PHP 的条件语句判断该字段的值,如果为 1,则不显示该行。
首先,需要在数据库表中添加一个布尔类型的字段,用于标识该行是否已被选中并提交。
ALTER TABLE your_table_name ADD COLUMN checkbox BOOLEAN DEFAULT 0;
将 your_table_name 替换为你的实际表名。 BOOLEAN 类型根据你的数据库系统可能需要调整,例如 MySQL 可以使用 TINYINT(1)。
在生成表格的 PHP 代码中,添加条件判断,根据 checkbox 字段的值来决定是否显示该行。
<form action="companies.php" method="post" onsubmit='checkform()'>
<table border=2 style="width:1200px;">
<?php
// 假设 $ff 是从数据库查询得到的一行数据
while ($ff = mysqli_fetch_assoc($result)) {
if($ff['checkbox'] == 0){ // 仅显示 checkbox 字段为 0 的行
?>
<tr>
<td class="ttd"><input type="checkbox" value="<?php echo $ff['ID']; ?>" name="chk[]"></td>
<td class="ttd"><?php echo htmlentities($ff['ID']); ?></td>
<td class="ttd"><?php echo htmlentities($ff['Invoice_number']); ?>
<input type="hidden" name="Inum[<?php echo $ff['ID']; ?>]" value="<?php echo $ff['Invoice_number']; ?>">
</td>
<td class="ttd"><?php echo htmlentities($ff['Invoice_date']); ?></td>
<td class="ttd"><?php echo htmlentities($ff['Month']); ?></td>
<td class="ttd"><?php echo htmlentities($ff['Space_name']); ?>
<input type="hidden" name="Sname[<?php echo $ff['ID']; ?>]" value="<?php echo $ff['Space_name']; ?>">
</td>
<td class="ttd"><?php echo htmlentities($ff['Company_Name']); ?>
<input type="hidden" name="Cname[<?php echo $ff['ID']; ?>]" value="<?php echo $ff['Company_Name']; ?>">
</td>
<td class="ttd"><?php echo htmlentities($ff['Amount']); ?>
<input type="hidden" name="amount[<?php echo $ff['ID']; ?>]" value="<?php echo $ff['Amount']; ?>">
</td>
<td class="ttd" style="width:200px;"><?php echo htmlentities($x); ?>
<input type="hidden" name="iban[<?php echo $ff['ID']; ?>]" value="<?php echo $ff['Iban']; ?>">
</td>
<td class="ttd"><?php echo htmlentities($ff['BIC']); ?>
<input type="hidden" name="bic[<?php echo $ff['ID']; ?>]" value="<?php echo $ff['BIC']; ?>">
</td>
</tr>
<?php
} // end if
} // end while
?>
</table>
<button type="submit" name="submit" value="submit" onclick='sendit()'>submit</button>
</form>在 companies.php 文件中,当处理完表单提交并生成 XML 文件后,更新数据库中对应行的 checkbox 字段。
if ($_POST['submit']){
#### XML file create
####..... at the End, when all xml attribute to be created ######
// 假设 $invoice_number 是当前处理的 invoice number
$invoice_number = $_POST['Inum'][$ff['ID']]; // 从POST获取发票号码
$query = "UPDATE your_table_name SET checkbox = 1 WHERE Invoice_number = '{$invoice_number}'";
$result = mysqli_query($connection, $query);
if($result && mysqli_affected_rows($connection)>= 0){
$xml->formatOutput = true;
$xml->save('../includes/xml/'.$filename) or die('XML Create Error') ;
redirect_to("manage_content.php");
}
}注意事项:
提供的 JavaScript 代码主要用于控制表单提交和复制文本到剪贴板,与隐藏表格行的核心逻辑关系不大。以下是代码的简要分析:
总结
通过以上步骤,就可以实现提交后隐藏表格中已选中的行的功能。核心在于数据库字段的更新和前端的条件判断显示。务必注意代码中的安全性和错误处理,并根据实际情况进行调整。
以上就是如何在提交后隐藏表格中已选中的行的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号