I'm trying to create a dynamic table with checkbox. The problem with my code now is that it only should insert the first data in year,code and age. Male and Female would be checkbox, but these two field seem unable to get the correct data, The checkbox should store 1 with it's check 0 if it's uncheck. Please let me know if you see the causes these error in my code, Thank You
POST PHP
if(isset($_POST['add'])){
$h = $_POST['h'];
if($h >= 0){
for($i=0; $i<=$h; $i++){
$year=intval($_GET['year']);
$code=intval($_GET['code']);
$age = $_POST['age'][$i];
$male = $_POST['male'][$i];
$female = $_POST['female'][$i];
$sql = "delete from remark_details WHERE year=:year";
$query = $dbh->prepare($sql);
$query -> bindParam(':year',$year, PDO::PARAM_STR);
$query -> execute();
$sql2 = "INSERT INTO remark_details (year,code,age,male,female) VALUES('year','code','$age','$male','$female')";
$query2 = $dbh -> prepare($sql2);
$query2->execute();
}
}
}
PHP
<?php
foreach($results as $result){?>
<tr data-row='0'>
<td colspan="2"><input class="tableBody" type="text" name="age[]" value="<?php echo $result->age; ?>" autocomplete="off" /></td>
<td colspan="2"><input class="tableBody" type="checkbox" name="male[]" value="1" autocomplete="off" /></td>
<td colspan="2"><input class="tableBody" type="checkbox" name="female[]" value="1" autocomplete="off"/></td>
<td>
<a href='javascript:void(0);' class='remove' style='cursor:pointer'>
<i class='material-icons' title='Delete item'>remove_circle_outline</i>
</a>
</td>
</tr>
<?php }} ?>
<input name='h' type='text' id='h' value=0 readonly hidden />
JavaScript
<script>
document.addEventListener('click', function(to) {
var rows={};
const getparent=(e,tag)=>{
let n=e.target;
while( n.tagName.toUpperCase()!=tag.toUpperCase() ){
if( n.nodeName==='BODY' )return false;
n=n.parentNode;
}
return n;
};
const datedifference=(d1,d2)=>{
return {
y:Math.abs( d2.getFullYear() - d1.getFullYear() ),
m:Math.abs( d2.getMonth() - d1.getMonth() +1 ),
d:( Math.abs( ( d2 - d1 ) / ( 1000 * 60 * 60 * 24 ) ) )
}
};
document.getElementById('tbl_history').addEventListener('change',function(e){
if( e.target!=e.currentTarget && e.target.nodeType==1 && e.target.tagName=='INPUT' && e.target.type=='month' ){
let parent=getparent(e,'tr');
let duration=parent.querySelector('input[type="text"][name="duration[]"]');
let row=parent.dataset.row;
if( !rows.hasOwnProperty( row ) )rows[ row ]={};
rows[ row ][ e.target.name ]=e.target.value;
if( Object.keys( rows[ row ] ).length==2 ){
let keys=Object.keys( rows[ row ] );
let d1=new Date( rows[ row ][ keys[0] ] );
let d2=new Date( rows[ row ][ keys[1] ] );
let obj=datedifference(d1,d2);
duration.value=[obj.y+' Years' , obj.m+' Months' ].join(' ');
}
}
});
document.getElementById('tbl_history').addEventListener('click',function(e){
if( e.target!=e.currentTarget && e.target.nodeType==1 && e.target.tagName=='I' ){
let p=getparent(e,'tr');
p.parentNode.removeChild(p);
}
});
});
var h=0;
function history() {
h++;
var html = `
<tr data-row='${h}'>
<td colspan="2"><input class="tableBody" type="text" name="age[]" autocomplete="off" required /></td>
<td colspan="2"><input class="tableBody" type="checkbox" name="male[]" value="1" autocomplete="off" /></td>
<td colspan="2"><input class="tableBody" type="checkbox" name="female[]" value="1" autocomplete="off"/></td>
<td>
<a href='javascript:void(0);' class='remove' style='cursor:pointer'>
<i class='material-icons' title='Delete item'>remove_circle_outline</i>
</a>
</td>
</tr>`;
document.getElementById('tbl_history').insertAdjacentHTML('beforeend',html);
document.getElementById('h').value=h;
}
</script>
add? Also why havemaleandfemaleas columns, just have agendercolumn....none of those are likely to address your current issue, just observations until more info is addedmaleandfemaleboth, or3and have agenderstable that maps labels to ids