
本文旨在提供一种解决方案,实现在提交包含复选框的表格后,不再显示之前选中的行。核心思路是在数据库中添加一个布尔类型的字段,用于标记已提交的行,并在页面加载时根据该字段的值来决定是否显示该行。该方案不删除数据,仅控制显示。
该方案的核心在于:
首先,需要在你的数据库表中添加一个 checkbox 字段。假设你的表名为 ff,可以使用以下 SQL 语句添加该字段:
ALTER TABLE ff ADD COLUMN checkbox BOOLEAN DEFAULT 0;
这条语句会在 ff 表中添加一个名为 checkbox 的布尔类型字段,并将其默认值设置为 0。
立即学习“PHP免费学习笔记(深入)”;
修改生成表格的 PHP 代码,使其只显示 checkbox 字段值为 0 的行。
<form action="companies.php" method="post" onsubmit='checkform()'>
<table border=2 style="width:1200px";>
<?php
// 查询数据库,获取数据
$query = "SELECT * FROM ff"; // Modify with where clause
$result = mysqli_query($connection, $query);
while($ff = mysqli_fetch_assoc($result)) {
if($ff['checkbox'] == 0){ // Only show rows where checkbox is 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 $kunde['BIC']; ?>"></td>
</tr>
<?php
}
}
?>
</table>
<button type="submit" name="submit" value="submit" onclick='sendit()'>submit</button>
</form>在上述代码中,添加了一个 if 语句来判断 checkbox 字段的值。只有当 checkbox 的值为 0 时,才会显示该行。
修改 companies.php 文件,在处理表单提交时,更新数据库中已提交行的 checkbox 字段值为 1。
if ($_POST['submit']){
#### XML file create
####..... at the End, when all xml attribute to be created ######
// Iterate through the selected checkboxes
if(isset($_POST['chk']) && is_array($_POST['chk'])) {
foreach($_POST['chk'] as $invoice_number) { // Assuming invoice_number is the unique identifier
$invoice_number = mysqli_real_escape_string($connection, $invoice_number); // Sanitize the input
// Update the checkbox field to 1 for the selected rows
$query = "UPDATE ff SET checkbox = 1 WHERE ID = '{$invoice_number}'"; // Assuming you are using ID as invoice_number
$result = mysqli_query($connection, $query);
if(!$result) {
// Handle the error
echo "Error updating checkbox: " . mysqli_error($connection);
}
}
}
$xml->formatOutput = true;
$xml->save('../includes/xml/'.$filename) or die('XML Create Error') ;
redirect_to("manage_content.php");
}这段代码首先检查 $_POST['submit'] 是否存在,然后遍历选中的复选框,并更新数据库中对应行的 checkbox 字段值为 1。 注意: 需要使用 mysqli_real_escape_string 对用户输入进行转义,以防止 SQL 注入攻击。另外,代码中假设 Invoice_number 是唯一的,如果不是,请使用更合适的唯一标识符(例如 ID)。
虽然主要逻辑在后端完成,但你也可以选择使用 JavaScript 来增强用户体验。例如,可以在提交后立即隐藏已选中的行,而无需刷新页面。
function sendit(){
send = 1;
console.log(send);
// Get all checked checkboxes
var checkedCheckboxes = document.querySelectorAll('input[name="chk[]"]:checked');
// Hide the rows corresponding to the checked checkboxes
checkedCheckboxes.forEach(function(checkbox) {
// Find the parent row (<tr>) and hide it
var row = checkbox.closest('tr');
if (row) {
row.style.display = 'none';
}
});
}这段 JavaScript 代码会在 sendit() 函数中,获取所有被选中的复选框,并找到它们所在的行,然后将这些行隐藏起来。 注意: 这种方式只是在客户端隐藏了行,实际上数据仍然存在于数据库中,并且在下次加载页面时,仍然会根据 checkbox 字段的值来决定是否显示。
通过在数据库中添加一个标记字段,并在页面显示和提交处理时根据该字段的值进行判断,可以实现提交后隐藏已选行的功能。这种方法既保留了数据,又可以方便地控制数据的显示,是一种简单而有效的解决方案。记住,安全性始终是第一位的,务必对用户输入进行适当的验证和转义。
以上就是提交后隐藏已选行:PHP和JavaScript实现方案的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号