Robert Grain Robert Grain - 3 months ago 7
jQuery Question

Checkbox Dynamic jquery when adding a new row

I am having a problem with checkboxes using jquery and php. I am adding rows of hours and I want to add a Overtime checkbox which is on the form but when I use the jquery to add the data to a new line to allow for adding multiple rows for adding to the database after I cant get it to add the checkbox my Form data is as below.

I want to add the checkbox (Is Checked / Not Checked) and also the value of 0 not checked and 1 if checked.

the field is called add_overtime_hours

<!-- this ext section is the Adding Hours on the jobsheet -->
<script type="text/javascript">
var rownumhours = 0;
function addhours(frm) {
rownumhours ++;
<!-- the next part creates the html to add dynamic fields into the Div hoursrow
var hoursrow = '<p id="rownumhours'+rownumhours+'">Start Time: <input type="text" name="add_start[]" size="4" value="'+frm.add_start.value+'"> Finish Time: <input type="text" name="add_finish[]" value="'+frm.add_finish.value+'"> Date: <input type="text" name="add_date[]" value="'+frm.add_date.value+'"> Overtime: <input type="checkbox" name="add_overtime_hours[]" size="1" value="'+frm.add_overtime_hours.value+' "> <input type="button" value="Remove" onclick="removeRow('+rownumhours+');"></p>';
<!-- the next part looks at the partsrow div lower in the page and then appends the values to the row variable above -->
jQuery('#hoursrow').append(hoursrow);
frm.add_start.value = '';
frm.add_finish.value = '';
frm.add_overtime_hours.value = 'N';
frm.add_date.value = '';
}

function removeRow(rnum) {
jQuery('#rownumhours'+rnum).remove();
}
</script>

<tr>
<td colspan="3" align="left">
<div id="hoursrow">
<p>
<span class="text">Hours Labour :</span></span>
</p>
<p>
<span class="headings"><span class="text"><span class="hours">Start Time:
<input type="time" name="add_start" size="4" />Finish Time:
<input type="time" name="add_finish" size="4" />Date:
<input type="date" name="add_date" size="4" />OVT:
<input type="checkbox" name="add_overtime_hours" id="add_overtime_hours" Value="1" size="1" maxlength="1" />
</span></span>
<span class="text"><span class="hours">
<input onclick="addhours(this.form);" type="button" value="Add Labour" />
</span></span>
</p>
<p class="headings"><span class="text"><span class="hours">
<span class="info">(This row will not be saved unless you click on "Add Labour" first) </span></span></span>
</p>
</div>
</td>
<td width="7"></td>
</tr>
<tr>
<td>&nbsp;</td>
</tr>


thank you for any help that can be given on what I am doing wrong.

thank you

Answer

You have some errors in your code.

I fixed them and so the final code is in the following snippet.

In order to distinguish the names of each field I used the following criteria:

name="add_finish[1]"
name="add_finish[2]"
......

Of course, each name has a different prefix according to the name of the field.

Therefore, you may use the names so organized on your server side.

var rownumhours = 0;

function addhours(obj, e) {
  rownumhours++;
  var hoursrow = '<p id="rownumhours' + rownumhours + '">Start Time: <input type="time" name="add_start[' + rownumhours + ']" size="4" value="' +
      $(obj).closest('span.headings').find('[name="add_start"]').val() + '">  Finish Time: <input type="time" name="add_finish[' + rownumhours + ']" value="' +
      $(obj).closest('span.headings').find('[name="add_finish"]').val() + '"> Date: <input type="date" name="add_date[' + rownumhours + ']" value="' +
      $(obj).closest('span.headings').find('[name="add_date"]').val() + '"> Overtime: <input type="checkbox" name="add_overtime_hours[' + rownumhours + ']" size="1" value="' +
      $(obj).closest('span.headings').find('[name="add_overtime_hours"]').val() + '"' +
      (($(obj).closest('span.headings').find('[name="add_overtime_hours"]').is(':checked')) ? 'checked' : '') +
      ' "> <input type="button" value="Remove" onclick="removeRow(' +
      rownumhours + ');"></p>';
  jQuery('#hoursrow').append(hoursrow);
}

function removeRow(rnum) {
  jQuery('#rownumhours' + rnum).remove();
}
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>

<table>
    <tbody>
    <tr>
        <td colspan="3" align="left">
            <div id="hoursrow">
                <td>&nbsp;</td>
                <p><span class="text">Hours Labour :</span></span></p>

                <p> <span class="headings"><span class="text"><span class="hours">Start Time:
          <input type="time" name="add_start" size="4"/>
          Finish Time:
          <input type="time" name="add_finish" size="4"/>
          Date:
          <input type="date" name="add_date" size="4"/>
          OVT:
          <input type="checkbox" name="add_overtime_hours" id="add_overtime_hours" Value="1" size="1" maxlength="1"/>
          </span></span><span class="text"><span class="hours">
        <input onclick="addhours(this, event);" type="button" value="Add Labour"/>
        </span></span></p>

                <p class="headings"><span class="text"><span class="hours">
          <span class="info">(This row will not be saved unless you click on "Add Labour" first) </span></span></span>
                </p>
            </div>
        </td>
        <td width="7"></td>
    </tr>
    </tbody>
</table>

An example on how you can get all add_overtime_hours is:

$myboxes = $_POST['add_overtime_hours'];
if(empty($myboxes)) {
     echo("You didn't select any boxes.");
} else {
    $i = count($myboxes);
    echo("You selected $i box(es): ");
    for($j=0; $j < $i; $j++) {
         echo($myboxes[$i] . " ");
    }
}
      Show_Overtime: <input type="text" name="add_overtime_hours[]" size="1" value="' + (($(obj).closest('span.headings').find('[name="add_overtime_hours"]').is(':checked')) ? 'checked' : '1' )' ">