将 5 列内的复选框的值保存到数据库

发布于 2024-12-19 13:53:20 字数 5143 浏览 0 评论 0原文

如何将 5 列内的复选框值保存到数据库(PHP 和 JQUERY)?

PHP 代码显示 6 列。有 5 列包含从数据库循环的复选框。

---PHP

        <table  id="tblISP" border="0">         
    <tr>
        <th style="width:20px">
        </th>
        <th>
            MenuID
        </th>

        <th>
            Menu
        </th>
        <th>
            Delete
        </th>
        <th>
            Upddate
        </th>
        <th>
            View
        </th>
        <th>
            Save
        </th>
    </tr>
    <tr>
        <td>ALL <input type="checkbox" class="menuiddd" name="checkmenuid" id="checkmenuid"/></td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>ALL<input type="checkbox" class="menuidddel" name="checkdelete" id="checkdelete"/></td>
        <td>ALL<input type="checkbox" class="menuiddupdate" name="checkupdate" id="checkupdate"/></td>
        <td>ALL<input type="checkbox" class="menuiddview" name="checkview" id="checkview"/></td>
        <td>ALL<input type="checkbox" class="menuiddsave" name="checksave" id="checksave"/></td>
    </tr>
                        <tr>
        <td>

                <input type="checkbox" class="menuiddd" name="idd[]" id="idd[]" value="<?php echo $row["mid"]; ?>"/>
        </td>
        <td>
            <?php echo $row["mid"]; ?>
        </td>
        <td>
            <?php echo $row["mname"]; ?>
        </td>    
        <td>
            <input type="checkbox" class="menuidddel" name="del[]" id="del[]" value="chkdel" />
        </td>
        <td>
            <input type="checkbox" class="menuiddupdate" name="upd[]" id="upd[]" value="chkupd" />
        </td>
        <td>
            <input type="checkbox" class="menuiddview"  name="vie[]" id="vie[]" value="chkvie" />
        </td>
        <td>
            <input type="checkbox" class="menuiddsave" name="sav[]" id="sav[]" value="chksav" />
        </td>
    </tr>

        <tr>
        <td colspan="3">
            <!--<a id="hlsa" href="javascript:exit(0);">Select All</a>
            <a id="hldsa" href="javascript:exit(0);">DeSelect All</a>-->
        </td>
    </tr>
    <tr>
        <td colspan="3" align="center">
            <hr/>
            <input type="button" value="Save" id="btSave" name="btSave" style="cursor:pointer;float:none" class="allbutton">
        </td>
    </tr>
    <tr>
        <td colspan="3"  id="dvProc1" align="center">

        </td>
    </tr>
    </table>Save check-box value within 5 columns to database

------------------------------------------------------------ --------Jquery------------

单击按钮保存时的 Jquery 反应,然后从复选框获取信息保存到数据库。

$(document).ready(function (){

            $("#btSave").unbind();
            $("#btSave").click(function(){          

                var idd = [];
                var del = [];
                var upd = [];
                var vie = [];
                var sav = [];

                $("input[name='idd[]']:checked").each(function() {
                  idd.push($(this).val());
                });

                $("input[name='del[]']:checked").each(function() {
                  del.push($(this).val());
                });

                $("input[name='upd[]']:checked").each(function() {
                  upd.push($(this).val());
                });

                $("input[name='vie[]']:checked").each(function() {
                  vie.push($(this).val());
                });

                $("input[name='sav[]']:checked").each(function() {
                  sav.push($(this).val());
                });


                var mainarray=[idd,del,upd,vie,sav];
                var transposed = mainarray.transpose();

                //-----------------------alert(str);--note--need return-------------
                alert(transposed.join(';'));

            })


            $("#checkmenuid").click(function(){
                $('.menuiddd').attr('checked', $( this ).is( ':checked' ) ? 'checked' : "");
            });


            $("#checkdelete").click(function(){
                $('.menuidddel').attr('checked', $( this ).is( ':checked' ) ? 'checked' : "");
            });

            $("#checkupdate").click(function(){
                $('.menuiddupdate').attr('checked', $( this ).is( ':checked' ) ? 'checked' : "");
            });


            $("#checkview").click(function(){
                $('.menuiddview').attr('checked', $( this ).is( ':checked' ) ? 'checked' : "");
            });


            $("#checksave").click(function(){
                $('.menuiddsave').attr('checked', $( this ).is( ':checked' ) ? 'checked' : "");
            });

});

</script>

How to Save value of check-box within 5 columns to database (PHP and JQUERY) ?

PHP code are dispay 6 columns. there 5 columns contain check-box that loop from database.

---PHP-----------------------------------

        <table  id="tblISP" border="0">         
    <tr>
        <th style="width:20px">
        </th>
        <th>
            MenuID
        </th>

        <th>
            Menu
        </th>
        <th>
            Delete
        </th>
        <th>
            Upddate
        </th>
        <th>
            View
        </th>
        <th>
            Save
        </th>
    </tr>
    <tr>
        <td>ALL <input type="checkbox" class="menuiddd" name="checkmenuid" id="checkmenuid"/></td>
        <td> </td>
        <td> </td>
        <td>ALL<input type="checkbox" class="menuidddel" name="checkdelete" id="checkdelete"/></td>
        <td>ALL<input type="checkbox" class="menuiddupdate" name="checkupdate" id="checkupdate"/></td>
        <td>ALL<input type="checkbox" class="menuiddview" name="checkview" id="checkview"/></td>
        <td>ALL<input type="checkbox" class="menuiddsave" name="checksave" id="checksave"/></td>
    </tr>
                        <tr>
        <td>

                <input type="checkbox" class="menuiddd" name="idd[]" id="idd[]" value="<?php echo $row["mid"]; ?>"/>
        </td>
        <td>
            <?php echo $row["mid"]; ?>
        </td>
        <td>
            <?php echo $row["mname"]; ?>
        </td>    
        <td>
            <input type="checkbox" class="menuidddel" name="del[]" id="del[]" value="chkdel" />
        </td>
        <td>
            <input type="checkbox" class="menuiddupdate" name="upd[]" id="upd[]" value="chkupd" />
        </td>
        <td>
            <input type="checkbox" class="menuiddview"  name="vie[]" id="vie[]" value="chkvie" />
        </td>
        <td>
            <input type="checkbox" class="menuiddsave" name="sav[]" id="sav[]" value="chksav" />
        </td>
    </tr>

        <tr>
        <td colspan="3">
            <!--<a id="hlsa" href="javascript:exit(0);">Select All</a>
            <a id="hldsa" href="javascript:exit(0);">DeSelect All</a>-->
        </td>
    </tr>
    <tr>
        <td colspan="3" align="center">
            <hr/>
            <input type="button" value="Save" id="btSave" name="btSave" style="cursor:pointer;float:none" class="allbutton">
        </td>
    </tr>
    <tr>
        <td colspan="3"  id="dvProc1" align="center">

        </td>
    </tr>
    </table>Save check-box value within 5 columns to database

------------------Jquery------------

Jquery reaction when click button save then get information from check box save to database.

$(document).ready(function (){

            $("#btSave").unbind();
            $("#btSave").click(function(){          

                var idd = [];
                var del = [];
                var upd = [];
                var vie = [];
                var sav = [];

                $("input[name='idd[]']:checked").each(function() {
                  idd.push($(this).val());
                });

                $("input[name='del[]']:checked").each(function() {
                  del.push($(this).val());
                });

                $("input[name='upd[]']:checked").each(function() {
                  upd.push($(this).val());
                });

                $("input[name='vie[]']:checked").each(function() {
                  vie.push($(this).val());
                });

                $("input[name='sav[]']:checked").each(function() {
                  sav.push($(this).val());
                });


                var mainarray=[idd,del,upd,vie,sav];
                var transposed = mainarray.transpose();

                //-----------------------alert(str);--note--need return-------------
                alert(transposed.join(';'));

            })


            $("#checkmenuid").click(function(){
                $('.menuiddd').attr('checked', $( this ).is( ':checked' ) ? 'checked' : "");
            });


            $("#checkdelete").click(function(){
                $('.menuidddel').attr('checked', $( this ).is( ':checked' ) ? 'checked' : "");
            });

            $("#checkupdate").click(function(){
                $('.menuiddupdate').attr('checked', $( this ).is( ':checked' ) ? 'checked' : "");
            });


            $("#checkview").click(function(){
                $('.menuiddview').attr('checked', $( this ).is( ':checked' ) ? 'checked' : "");
            });


            $("#checksave").click(function(){
                $('.menuiddsave').attr('checked', $( this ).is( ':checked' ) ? 'checked' : "");
            });

});

</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一身仙ぐ女味 2024-12-26 13:53:20

我不完全确定我已经确定了你的问题,但在处理任何 jQuery 之前,你应该构建一个没有任何 javascript 的功能系统。您可以简单地正常编写它,然后添加干净的 JavaScript 层来处理异步提交,而不是定义一个非标准方法来处理表单提交。

首先,您需要将输入元素包装在表单元素中,然后编写 PHP 脚本来分析全局 $_POST 数组并根据需要执行各种数据库命令。您可以使用此 PHP 层来确定应根据用户在表单上选择的内容对数据库进行哪些调用的逻辑。请注意,未选中的复选框实际上不会在 POST 请求中发送,因此您必须在 PHP 中使用 isset() 来了解它们是否未选中。

一旦安排好,您就可以使用 jQuery 异步提交表单,并且不会导致页面刷新:

$(function() {
    $('form#yourformid').submit(function(event) {
        event.preventDefault(); // stop the page from redirecting

        // serialise your form's data, and send it in a POST request
        // to the form's action attribute
        $.post(
            $(this).attr('action'),
            $(this).serialize(),
            function() {
                alert('Data successfully saved.');
            });
    });
});

I'm not entirely sure I've identified your question, but before you deal with any jQuery you should build a functioning system without any javascript. Rather than defining a non-standard method for dealing with the form's submission, you can simply write it normally before adding a clean layer of javascript to handle asynchronous submission.

Firstly, you need to wrap your input elements in a form element, and then write a PHP script to analyse the global $_POST array and to execute the various database commands as required. You can use this PHP layer for the logic of identifying what calls should be made to the database, based on what the user has selected on the form. Note that checkboxes that are unchecked are not actually sent in the POST request, so you'll have to use isset() in PHP to know if they were unchecked.

Once that's arranged, you can use jQuery to submit the form asynchronously, and you won't cause a page refresh:

$(function() {
    $('form#yourformid').submit(function(event) {
        event.preventDefault(); // stop the page from redirecting

        // serialise your form's data, and send it in a POST request
        // to the form's action attribute
        $.post(
            $(this).attr('action'),
            $(this).serialize(),
            function() {
                alert('Data successfully saved.');
            });
    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文