简体   繁体   English

更改数组中的动态复选框以更新 mysql 数据库

[英]Changing a dynamic checkbox in an array to update a mysql Database

Hello I am really struggling with checkboxes I have a form that brings in Hours details from a mysql table and I have an overtime checkbox it works when adding new Hours but I want to be able to save a change in the checkbox back to the database eg the manger doesn't agree it is overtime.您好,我真的很纠结于复选框 我有一个表单,可以从 mysql 表中导入小时的详细信息,并且我有一个加班复选框,它在添加新小时时有效,但我希望能够将复选框中的更改保存回数据库,例如马槽不同意这是加班。 as you see below.正如你在下面看到的。

It is read into an array and So I don't understand how to make the change to the text box to the right named chg_hours_show_ovt.它被读入一个数组,所以我不明白如何更改右侧名为 chg_hours_show_ovt 的文本框。 which will be hidden and then updated on the Database这将被隐藏,然后在数据库上更新

显示复选框

I have tried the following but I'm not sure if I'm on the right lines and any help would be very grateful.我已经尝试了以下操作,但我不确定我是否在正确的路线上,任何帮助将不胜感激。

I bring in the form from the database as follows.我从数据库中引入表单如下。 I didnt want the .checked really because it might not be and usually won't be but I didn't know how else to do it eg .val ?我真的不想要 .checked ,因为它可能不是,通常也不会,但我不知道怎么做,例如 .val ? but What ever I do it doesn't change at all.但我所做的一切都没有改变。 I'm a little worried what I have here will change every checkbox ?我有点担心我这里的东西会改变每个复选框?

I'm very sorry i'm still very new to all of the languages especially java and jquery.很抱歉,我对所有语言仍然很陌生,尤其是 java 和 jquery。 Thank you谢谢

    $('#chg_hours_ovt').on('change',function() {
      if($(this).checked) {
        $('#chg_hours_show_ovt').val($('#chg_hours_ovt').val());
      }
});


</script>

. .

            while ($list_hours = mysqli_fetch_array($hours_result))
            {
                echo " <table border ='0' table width = 95%>";
                echo '<td> <input type="hidden" name="chg_hrs_id[]" value="'.$list_hours['unique_id'].'">';
                echo '<td> Start Time  <input type="time" name="chg_hrs_str[]" value="'.$list_hours['start_time'].'" >';
                echo '<td> Finish Time <input type="time" name="chg_hrs_fin[]" value="'.$list_hours['finish_time'].'">';
                echo '<td> Date Of Work <input type="date" name="chg_hrs_date[]" value="'.$list_hours['date'].'">';
                echo '<td> Overtime <input type="checkbox" name="chg_hrs_ovt[]" '.($list_hours["overtime"]==1 ? 'checked="checked"' : ''). '>';  
                echo '<td> Overtime <input type="text" name="chg_hrs_show_ovt[]" value=' .($list_hours["overtime"]==1 ? '1' : '0').'>'; 
                echo "</tr>"; 

            }
            echo "</table>";    

Try this:尝试这个:

    $("input[name='chg_hrs_ovt[]']").on('change', function () {
        var bValue = $(this).is(':checked') ? 1 : 0;
        //get current row
         var row = $(this).closest('tr');
         //find overtime
         row.find("input[name='chg_hrs_show_ovt[]']").val(bValue);       
    });

However I guess that you dont need the variable 'chg_hrs_show_ovt[]' , you could sent just 'chg_hrs_ovt[]' and catch it server-side.但是我想您不需要变量 'chg_hrs_show_ovt[]' ,您可以只发送 'chg_hrs_ovt[]' 并在服务器端捕获它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM