Reputation: 61
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
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>";
Upvotes: 1
Views: 474
Reputation: 1305
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.
Upvotes: 1