提交后隐藏已选行:PHP和JavaScript实现方案

聖光之護
发布: 2025-09-28 16:44:34
原创
412人浏览过

提交后隐藏已选行:php和javascript实现方案

本文旨在提供一种解决方案,实现在提交包含复选框的表格后,不再显示之前选中的行。核心思路是在数据库中添加一个布尔类型的字段,用于标记已提交的行,并在页面加载时根据该字段的值来决定是否显示该行。该方案不删除数据,仅控制显示。

方案概述

该方案的核心在于:

  1. 数据库改造: 在存储表格数据的数据库表中,添加一个布尔类型的字段(例如 checkbox),用于标记该行是否已被选中并提交。初始值设置为 NULL 或 0。
  2. 提交处理: 在处理表单提交的 PHP 脚本中,更新数据库中已提交行的 checkbox 字段值为 1。
  3. 数据显示: 在生成表格的 PHP 代码中,根据 checkbox 字段的值,决定是否显示该行。

详细步骤

1. 数据库表结构修改

首先,需要在你的数据库表中添加一个 checkbox 字段。假设你的表名为 ff,可以使用以下 SQL 语句添加该字段:

ALTER TABLE ff ADD COLUMN checkbox BOOLEAN DEFAULT 0;
登录后复制

这条语句会在 ff 表中添加一个名为 checkbox 的布尔类型字段,并将其默认值设置为 0。

立即学习PHP免费学习笔记(深入)”;

2. 修改表单显示逻辑

修改生成表格的 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 时,才会显示该行。

火山方舟
火山方舟

火山引擎一站式大模型服务平台,已接入满血版DeepSeek

火山方舟 99
查看详情 火山方舟

3. 修改提交处理逻辑

修改 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)。

4. 客户端 JavaScript (可选)

虽然主要逻辑在后端完成,但你也可以选择使用 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 字段的值来决定是否显示。

注意事项

  • 安全性: 务必对用户输入进行转义,以防止 SQL 注入攻击。
  • 错误处理: 在执行数据库操作时,添加适当的错误处理机制。
  • 唯一标识符: 确保使用正确的唯一标识符来更新数据库中的记录。
  • 性能: 如果数据量很大,可以考虑使用索引来优化查询性能。

总结

通过在数据库中添加一个标记字段,并在页面显示和提交处理时根据该字段的值进行判断,可以实现提交后隐藏已选行的功能。这种方法既保留了数据,又可以方便地控制数据的显示,是一种简单而有效的解决方案。记住,安全性始终是第一位的,务必对用户输入进行适当的验证和转义。

以上就是提交后隐藏已选行:PHP和JavaScript实现方案的详细内容,更多请关注php中文网其它相关文章!

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号