1

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 e.g. the manger doesn't agree it is overtime. 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. which will be hidden and then updated on the Database

Showing the checkboxes

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 e.g. .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. 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>";    
3
  • first: with $('#chg_hours_ovt') you select all elements with that id, not name. I would change it to a class-selector and then find the last column in that table. Commented Jan 21, 2017 at 0:23
  • second: you don't need to dublicate the value of chg_hrs_ovt to ...show_ovt: The checkbox-value will also be sent to server. And you could make a listener on a change of that checkbox to update db. Commented Jan 21, 2017 at 0:25
  • 1
    "especially java" - nitpicking here, but javascript is very different to java; be careful that you don't get those two mixed up. Similarly jQuery isn't a language - it's a javascript library. Commented Jan 21, 2017 at 0:26

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Good morning recobar thank you very much for that I have tried it but it doesn't make any changes when clicking the overtime checkbox.
Have a look to the result: jsfiddle.net/edu_guerr/9g14gg29/1 , you must add jquery as external resource, for example: ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js
Hello rescobar thank you very much for replying. it was the jquery library which is odd because I have <!-- This includes the jquery javascript library --> <script type="text/javascript" src="js/jquery-2.1.3.min.js"></script> and it didn't work which i downloaded it to my site, but then I have added the later library that you suggested but the other part of my site stopped to use the adding of fields but with both your function works and the existing other function I have works but I'm not sure if it should have both running or it will cause issues.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.